Argyrios Kyrtzidis | 647dcd8 | 2012-03-05 05:48:17 +0000 | [diff] [blame] | 1 | //===- unittests/Lex/PreprocessingRecordTest.cpp - PreprocessingRecord tests =// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "clang/Basic/SourceManager.h" |
| 11 | #include "clang/Basic/FileManager.h" |
| 12 | #include "clang/Basic/Diagnostic.h" |
| 13 | #include "clang/Basic/LangOptions.h" |
| 14 | #include "clang/Basic/TargetOptions.h" |
| 15 | #include "clang/Basic/TargetInfo.h" |
| 16 | #include "clang/Lex/ModuleLoader.h" |
| 17 | #include "clang/Lex/HeaderSearch.h" |
| 18 | #include "clang/Lex/Preprocessor.h" |
| 19 | #include "clang/Lex/PreprocessingRecord.h" |
| 20 | #include "llvm/Config/config.h" |
| 21 | |
| 22 | #include "gtest/gtest.h" |
| 23 | |
| 24 | using namespace llvm; |
| 25 | using namespace clang; |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | // The test fixture. |
| 30 | class PreprocessingRecordTest : public ::testing::Test { |
| 31 | protected: |
| 32 | PreprocessingRecordTest() |
| 33 | : FileMgr(FileMgrOpts), |
| 34 | DiagID(new DiagnosticIDs()), |
| 35 | Diags(DiagID, new IgnoringDiagConsumer()), |
Douglas Gregor | 44d6361 | 2012-10-17 00:11:35 +0000 | [diff] [blame] | 36 | SourceMgr(Diags, FileMgr), |
| 37 | TargetOpts(new TargetOptions) |
| 38 | { |
| 39 | TargetOpts->Triple = "x86_64-apple-darwin11.1.0"; |
| 40 | Target = TargetInfo::CreateTargetInfo(Diags, *TargetOpts); |
Argyrios Kyrtzidis | 647dcd8 | 2012-03-05 05:48:17 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | FileSystemOptions FileMgrOpts; |
| 44 | FileManager FileMgr; |
| 45 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID; |
| 46 | DiagnosticsEngine Diags; |
| 47 | SourceManager SourceMgr; |
| 48 | LangOptions LangOpts; |
Douglas Gregor | 44d6361 | 2012-10-17 00:11:35 +0000 | [diff] [blame] | 49 | IntrusiveRefCntPtr<TargetOptions> TargetOpts; |
Argyrios Kyrtzidis | 647dcd8 | 2012-03-05 05:48:17 +0000 | [diff] [blame] | 50 | IntrusiveRefCntPtr<TargetInfo> Target; |
| 51 | }; |
| 52 | |
| 53 | class VoidModuleLoader : public ModuleLoader { |
| 54 | virtual Module *loadModule(SourceLocation ImportLoc, ModuleIdPath Path, |
| 55 | Module::NameVisibilityKind Visibility, |
| 56 | bool IsInclusionDirective) { |
| 57 | return 0; |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | TEST_F(PreprocessingRecordTest, PPRecAPI) { |
| 62 | const char *source = |
| 63 | "0 1\n" |
| 64 | "#if 1\n" |
| 65 | "2\n" |
| 66 | "#ifndef BB\n" |
| 67 | "3 4\n" |
| 68 | "#else\n" |
| 69 | "#endif\n" |
| 70 | "5\n" |
| 71 | "#endif\n" |
| 72 | "6\n" |
| 73 | "#if 1\n" |
| 74 | "7\n" |
| 75 | "#if 1\n" |
| 76 | "#endif\n" |
| 77 | "8\n" |
| 78 | "#endif\n" |
| 79 | "9\n"; |
| 80 | |
| 81 | MemoryBuffer *buf = MemoryBuffer::getMemBuffer(source); |
| 82 | SourceMgr.createMainFileIDForMemBuffer(buf); |
| 83 | |
| 84 | VoidModuleLoader ModLoader; |
| 85 | HeaderSearch HeaderInfo(FileMgr, Diags, LangOpts, Target.getPtr()); |
| 86 | Preprocessor PP(Diags, LangOpts, |
| 87 | Target.getPtr(), |
| 88 | SourceMgr, HeaderInfo, ModLoader, |
| 89 | /*IILookup =*/ 0, |
| 90 | /*OwnsHeaderSearch =*/false, |
| 91 | /*DelayInitialization =*/ false); |
| 92 | PP.createPreprocessingRecord(true); |
| 93 | PP.EnterMainSourceFile(); |
| 94 | |
| 95 | std::vector<Token> toks; |
| 96 | while (1) { |
| 97 | Token tok; |
| 98 | PP.Lex(tok); |
| 99 | if (tok.is(tok::eof)) |
| 100 | break; |
| 101 | toks.push_back(tok); |
| 102 | } |
| 103 | |
| 104 | // Make sure we got the tokens that we expected. |
| 105 | ASSERT_EQ(10U, toks.size()); |
| 106 | |
| 107 | PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); |
| 108 | EXPECT_FALSE(PPRec.rangeIntersectsConditionalDirective( |
| 109 | SourceRange(toks[0].getLocation(), toks[1].getLocation()))); |
| 110 | EXPECT_TRUE(PPRec.rangeIntersectsConditionalDirective( |
| 111 | SourceRange(toks[0].getLocation(), toks[2].getLocation()))); |
| 112 | EXPECT_FALSE(PPRec.rangeIntersectsConditionalDirective( |
| 113 | SourceRange(toks[3].getLocation(), toks[4].getLocation()))); |
| 114 | EXPECT_TRUE(PPRec.rangeIntersectsConditionalDirective( |
| 115 | SourceRange(toks[1].getLocation(), toks[5].getLocation()))); |
| 116 | EXPECT_TRUE(PPRec.rangeIntersectsConditionalDirective( |
| 117 | SourceRange(toks[2].getLocation(), toks[6].getLocation()))); |
| 118 | EXPECT_FALSE(PPRec.rangeIntersectsConditionalDirective( |
| 119 | SourceRange(toks[2].getLocation(), toks[5].getLocation()))); |
| 120 | EXPECT_FALSE(PPRec.rangeIntersectsConditionalDirective( |
| 121 | SourceRange(toks[0].getLocation(), toks[6].getLocation()))); |
| 122 | EXPECT_TRUE(PPRec.rangeIntersectsConditionalDirective( |
| 123 | SourceRange(toks[2].getLocation(), toks[8].getLocation()))); |
| 124 | EXPECT_FALSE(PPRec.rangeIntersectsConditionalDirective( |
| 125 | SourceRange(toks[0].getLocation(), toks[9].getLocation()))); |
| 126 | |
| 127 | EXPECT_TRUE(PPRec.areInDifferentConditionalDirectiveRegion( |
| 128 | toks[0].getLocation(), toks[2].getLocation())); |
| 129 | EXPECT_FALSE(PPRec.areInDifferentConditionalDirectiveRegion( |
| 130 | toks[3].getLocation(), toks[4].getLocation())); |
| 131 | EXPECT_TRUE(PPRec.areInDifferentConditionalDirectiveRegion( |
| 132 | toks[1].getLocation(), toks[5].getLocation())); |
| 133 | EXPECT_TRUE(PPRec.areInDifferentConditionalDirectiveRegion( |
| 134 | toks[2].getLocation(), toks[0].getLocation())); |
| 135 | EXPECT_FALSE(PPRec.areInDifferentConditionalDirectiveRegion( |
| 136 | toks[4].getLocation(), toks[3].getLocation())); |
| 137 | EXPECT_TRUE(PPRec.areInDifferentConditionalDirectiveRegion( |
| 138 | toks[5].getLocation(), toks[1].getLocation())); |
| 139 | } |
| 140 | |
| 141 | } // anonymous namespace |