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