Argyrios Kyrtzidis | 2403797 | 2011-12-21 16:56:29 +0000 | [diff] [blame] | 1 | //===- unittests/Basic/SourceManagerTest.cpp ------ SourceManager 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" |
Argyrios Kyrtzidis | 2403797 | 2011-12-21 16:56:29 +0000 | [diff] [blame] | 11 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | edf8e38 | 2012-10-23 22:38:58 +0000 | [diff] [blame] | 12 | #include "clang/Basic/DiagnosticOptions.h" |
Chandler Carruth | 320d966 | 2012-12-04 09:45:34 +0000 | [diff] [blame] | 13 | #include "clang/Basic/FileManager.h" |
Argyrios Kyrtzidis | 2403797 | 2011-12-21 16:56:29 +0000 | [diff] [blame] | 14 | #include "clang/Basic/LangOptions.h" |
Argyrios Kyrtzidis | 2403797 | 2011-12-21 16:56:29 +0000 | [diff] [blame] | 15 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 320d966 | 2012-12-04 09:45:34 +0000 | [diff] [blame] | 16 | #include "clang/Basic/TargetOptions.h" |
Argyrios Kyrtzidis | 2403797 | 2011-12-21 16:56:29 +0000 | [diff] [blame] | 17 | #include "clang/Lex/HeaderSearch.h" |
Douglas Gregor | 40ba1a0 | 2012-10-24 16:24:38 +0000 | [diff] [blame] | 18 | #include "clang/Lex/HeaderSearchOptions.h" |
Chandler Carruth | 320d966 | 2012-12-04 09:45:34 +0000 | [diff] [blame] | 19 | #include "clang/Lex/ModuleLoader.h" |
Argyrios Kyrtzidis | 2403797 | 2011-12-21 16:56:29 +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" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Alp Toker | 1d257e1 | 2014-06-04 03:28:55 +0000 | [diff] [blame] | 23 | #include "llvm/Config/llvm-config.h" |
Argyrios Kyrtzidis | 2403797 | 2011-12-21 16:56:29 +0000 | [diff] [blame] | 24 | #include "gtest/gtest.h" |
| 25 | |
Argyrios Kyrtzidis | 2403797 | 2011-12-21 16:56:29 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | // The test fixture. |
| 31 | class SourceManagerTest : public ::testing::Test { |
| 32 | protected: |
| 33 | SourceManagerTest() |
| 34 | : FileMgr(FileMgrOpts), |
| 35 | DiagID(new DiagnosticIDs()), |
Douglas Gregor | d8cfd39 | 2012-10-23 22:31:51 +0000 | [diff] [blame] | 36 | Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()), |
Douglas Gregor | 44d6361 | 2012-10-17 00:11:35 +0000 | [diff] [blame] | 37 | SourceMgr(Diags, FileMgr), |
| 38 | TargetOpts(new TargetOptions) { |
| 39 | TargetOpts->Triple = "x86_64-apple-darwin11.1.0"; |
Alp Toker | 8075808 | 2014-07-06 05:26:44 +0000 | [diff] [blame] | 40 | Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts); |
Argyrios Kyrtzidis | 2403797 | 2011-12-21 16:56:29 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | FileSystemOptions FileMgrOpts; |
| 44 | FileManager FileMgr; |
Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 45 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID; |
Argyrios Kyrtzidis | 2403797 | 2011-12-21 16:56:29 +0000 | [diff] [blame] | 46 | DiagnosticsEngine Diags; |
| 47 | SourceManager SourceMgr; |
| 48 | LangOptions LangOpts; |
Alp Toker | 8075808 | 2014-07-06 05:26:44 +0000 | [diff] [blame] | 49 | std::shared_ptr<TargetOptions> TargetOpts; |
Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 50 | IntrusiveRefCntPtr<TargetInfo> Target; |
Argyrios Kyrtzidis | 2403797 | 2011-12-21 16:56:29 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | class VoidModuleLoader : public ModuleLoader { |
John Thompson | 2d94bbb | 2014-04-23 19:04:32 +0000 | [diff] [blame] | 54 | ModuleLoadResult loadModule(SourceLocation ImportLoc, |
| 55 | ModuleIdPath Path, |
| 56 | Module::NameVisibilityKind Visibility, |
| 57 | bool IsInclusionDirective) override { |
Douglas Gregor | 8c05893 | 2012-11-30 00:01:57 +0000 | [diff] [blame] | 58 | return ModuleLoadResult(); |
Argyrios Kyrtzidis | 2403797 | 2011-12-21 16:56:29 +0000 | [diff] [blame] | 59 | } |
NAKAMURA Takumi | e73d2a9 | 2013-01-12 02:16:29 +0000 | [diff] [blame] | 60 | |
John Thompson | 2d94bbb | 2014-04-23 19:04:32 +0000 | [diff] [blame] | 61 | void makeModuleVisible(Module *Mod, |
| 62 | Module::NameVisibilityKind Visibility, |
Richard Smith | a7e2cc6 | 2015-05-01 01:53:09 +0000 | [diff] [blame] | 63 | SourceLocation ImportLoc) override { } |
John Thompson | 2255f2c | 2014-04-23 12:57:01 +0000 | [diff] [blame] | 64 | |
John Thompson | 2d94bbb | 2014-04-23 19:04:32 +0000 | [diff] [blame] | 65 | GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 66 | { return nullptr; } |
John Thompson | 2d94bbb | 2014-04-23 19:04:32 +0000 | [diff] [blame] | 67 | bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 68 | { return 0; } |
Argyrios Kyrtzidis | 2403797 | 2011-12-21 16:56:29 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | TEST_F(SourceManagerTest, isBeforeInTranslationUnit) { |
| 72 | const char *source = |
| 73 | "#define M(x) [x]\n" |
| 74 | "M(foo)"; |
James Y Knight | b214cbc | 2016-03-04 19:00:41 +0000 | [diff] [blame] | 75 | std::unique_ptr<llvm::MemoryBuffer> Buf = |
| 76 | llvm::MemoryBuffer::getMemBuffer(source); |
David Blaikie | 50a5f97 | 2014-08-29 07:59:55 +0000 | [diff] [blame] | 77 | FileID mainFileID = SourceMgr.createFileID(std::move(Buf)); |
Alp Toker | b671e34 | 2014-05-21 01:12:41 +0000 | [diff] [blame] | 78 | SourceMgr.setMainFileID(mainFileID); |
Argyrios Kyrtzidis | 2403797 | 2011-12-21 16:56:29 +0000 | [diff] [blame] | 79 | |
| 80 | VoidModuleLoader ModLoader; |
David Blaikie | 9c28cb3 | 2017-01-06 01:04:46 +0000 | [diff] [blame] | 81 | HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr, |
| 82 | Diags, LangOpts, &*Target); |
David Blaikie | e304168 | 2017-01-05 19:11:36 +0000 | [diff] [blame] | 83 | Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts, |
| 84 | SourceMgr, HeaderInfo, ModLoader, |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 85 | /*IILookup =*/nullptr, |
Alp Toker | 1ae02f6 | 2014-05-02 03:43:30 +0000 | [diff] [blame] | 86 | /*OwnsHeaderSearch =*/false); |
| 87 | PP.Initialize(*Target); |
Argyrios Kyrtzidis | 2403797 | 2011-12-21 16:56:29 +0000 | [diff] [blame] | 88 | PP.EnterMainSourceFile(); |
| 89 | |
| 90 | std::vector<Token> toks; |
| 91 | while (1) { |
| 92 | Token tok; |
| 93 | PP.Lex(tok); |
| 94 | if (tok.is(tok::eof)) |
| 95 | break; |
| 96 | toks.push_back(tok); |
| 97 | } |
| 98 | |
| 99 | // Make sure we got the tokens that we expected. |
| 100 | ASSERT_EQ(3U, toks.size()); |
| 101 | ASSERT_EQ(tok::l_square, toks[0].getKind()); |
| 102 | ASSERT_EQ(tok::identifier, toks[1].getKind()); |
| 103 | ASSERT_EQ(tok::r_square, toks[2].getKind()); |
| 104 | |
| 105 | SourceLocation lsqrLoc = toks[0].getLocation(); |
| 106 | SourceLocation idLoc = toks[1].getLocation(); |
| 107 | SourceLocation rsqrLoc = toks[2].getLocation(); |
| 108 | |
| 109 | SourceLocation macroExpStartLoc = SourceMgr.translateLineCol(mainFileID, 2, 1); |
| 110 | SourceLocation macroExpEndLoc = SourceMgr.translateLineCol(mainFileID, 2, 6); |
| 111 | ASSERT_TRUE(macroExpStartLoc.isFileID()); |
| 112 | ASSERT_TRUE(macroExpEndLoc.isFileID()); |
| 113 | |
Dylan Noblesmith | f1a13f2 | 2012-02-13 12:32:26 +0000 | [diff] [blame] | 114 | SmallString<32> str; |
Argyrios Kyrtzidis | 2403797 | 2011-12-21 16:56:29 +0000 | [diff] [blame] | 115 | ASSERT_EQ("M", PP.getSpelling(macroExpStartLoc, str)); |
| 116 | ASSERT_EQ(")", PP.getSpelling(macroExpEndLoc, str)); |
| 117 | |
| 118 | EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(lsqrLoc, idLoc)); |
| 119 | EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, rsqrLoc)); |
| 120 | EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(macroExpStartLoc, idLoc)); |
| 121 | EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, macroExpEndLoc)); |
| 122 | } |
| 123 | |
Jordan Rose | 8d63d5b | 2012-06-19 03:09:38 +0000 | [diff] [blame] | 124 | TEST_F(SourceManagerTest, getColumnNumber) { |
| 125 | const char *Source = |
| 126 | "int x;\n" |
| 127 | "int y;"; |
| 128 | |
James Y Knight | b214cbc | 2016-03-04 19:00:41 +0000 | [diff] [blame] | 129 | std::unique_ptr<llvm::MemoryBuffer> Buf = |
| 130 | llvm::MemoryBuffer::getMemBuffer(Source); |
David Blaikie | 50a5f97 | 2014-08-29 07:59:55 +0000 | [diff] [blame] | 131 | FileID MainFileID = SourceMgr.createFileID(std::move(Buf)); |
Alp Toker | b671e34 | 2014-05-21 01:12:41 +0000 | [diff] [blame] | 132 | SourceMgr.setMainFileID(MainFileID); |
Jordan Rose | 8d63d5b | 2012-06-19 03:09:38 +0000 | [diff] [blame] | 133 | |
| 134 | bool Invalid; |
| 135 | |
| 136 | Invalid = false; |
| 137 | EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, &Invalid)); |
| 138 | EXPECT_TRUE(!Invalid); |
| 139 | |
| 140 | Invalid = false; |
| 141 | EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 4, &Invalid)); |
| 142 | EXPECT_TRUE(!Invalid); |
| 143 | |
| 144 | Invalid = false; |
| 145 | EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 7, &Invalid)); |
| 146 | EXPECT_TRUE(!Invalid); |
| 147 | |
| 148 | Invalid = false; |
| 149 | EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 11, &Invalid)); |
| 150 | EXPECT_TRUE(!Invalid); |
| 151 | |
| 152 | Invalid = false; |
| 153 | EXPECT_EQ(7U, SourceMgr.getColumnNumber(MainFileID, strlen(Source), |
| 154 | &Invalid)); |
| 155 | EXPECT_TRUE(!Invalid); |
| 156 | |
| 157 | Invalid = false; |
| 158 | SourceMgr.getColumnNumber(MainFileID, strlen(Source)+1, &Invalid); |
| 159 | EXPECT_TRUE(Invalid); |
| 160 | |
| 161 | // Test invalid files |
| 162 | Invalid = false; |
| 163 | SourceMgr.getColumnNumber(FileID(), 0, &Invalid); |
| 164 | EXPECT_TRUE(Invalid); |
| 165 | |
| 166 | Invalid = false; |
| 167 | SourceMgr.getColumnNumber(FileID(), 1, &Invalid); |
| 168 | EXPECT_TRUE(Invalid); |
| 169 | |
| 170 | // Test with no invalid flag. |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 171 | EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, nullptr)); |
Jordan Rose | 8d63d5b | 2012-06-19 03:09:38 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Argyrios Kyrtzidis | e841c90 | 2011-12-21 16:56:35 +0000 | [diff] [blame] | 174 | #if defined(LLVM_ON_UNIX) |
| 175 | |
| 176 | TEST_F(SourceManagerTest, getMacroArgExpandedLocation) { |
| 177 | const char *header = |
| 178 | "#define FM(x,y) x\n"; |
| 179 | |
| 180 | const char *main = |
| 181 | "#include \"/test-header.h\"\n" |
| 182 | "#define VAL 0\n" |
| 183 | "FM(VAL,0)\n" |
| 184 | "FM(0,VAL)\n" |
| 185 | "FM(FM(0,VAL),0)\n" |
| 186 | "#define CONCAT(X, Y) X##Y\n" |
| 187 | "CONCAT(1,1)\n"; |
| 188 | |
James Y Knight | b214cbc | 2016-03-04 19:00:41 +0000 | [diff] [blame] | 189 | std::unique_ptr<llvm::MemoryBuffer> HeaderBuf = |
| 190 | llvm::MemoryBuffer::getMemBuffer(header); |
| 191 | std::unique_ptr<llvm::MemoryBuffer> MainBuf = |
| 192 | llvm::MemoryBuffer::getMemBuffer(main); |
David Blaikie | 50a5f97 | 2014-08-29 07:59:55 +0000 | [diff] [blame] | 193 | FileID mainFileID = SourceMgr.createFileID(std::move(MainBuf)); |
Alp Toker | b671e34 | 2014-05-21 01:12:41 +0000 | [diff] [blame] | 194 | SourceMgr.setMainFileID(mainFileID); |
Argyrios Kyrtzidis | e841c90 | 2011-12-21 16:56:35 +0000 | [diff] [blame] | 195 | |
| 196 | const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h", |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 197 | HeaderBuf->getBufferSize(), 0); |
David Blaikie | 49cc318 | 2014-08-27 20:54:45 +0000 | [diff] [blame] | 198 | SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf)); |
Argyrios Kyrtzidis | e841c90 | 2011-12-21 16:56:35 +0000 | [diff] [blame] | 199 | |
| 200 | VoidModuleLoader ModLoader; |
David Blaikie | 9c28cb3 | 2017-01-06 01:04:46 +0000 | [diff] [blame] | 201 | HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr, |
| 202 | Diags, LangOpts, &*Target); |
David Blaikie | e304168 | 2017-01-05 19:11:36 +0000 | [diff] [blame] | 203 | Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts, |
| 204 | SourceMgr, HeaderInfo, ModLoader, |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 205 | /*IILookup =*/nullptr, |
Alp Toker | 1ae02f6 | 2014-05-02 03:43:30 +0000 | [diff] [blame] | 206 | /*OwnsHeaderSearch =*/false); |
| 207 | PP.Initialize(*Target); |
Argyrios Kyrtzidis | e841c90 | 2011-12-21 16:56:35 +0000 | [diff] [blame] | 208 | PP.EnterMainSourceFile(); |
| 209 | |
| 210 | std::vector<Token> toks; |
| 211 | while (1) { |
| 212 | Token tok; |
| 213 | PP.Lex(tok); |
| 214 | if (tok.is(tok::eof)) |
| 215 | break; |
| 216 | toks.push_back(tok); |
| 217 | } |
| 218 | |
| 219 | // Make sure we got the tokens that we expected. |
| 220 | ASSERT_EQ(4U, toks.size()); |
| 221 | ASSERT_EQ(tok::numeric_constant, toks[0].getKind()); |
| 222 | ASSERT_EQ(tok::numeric_constant, toks[1].getKind()); |
| 223 | ASSERT_EQ(tok::numeric_constant, toks[2].getKind()); |
| 224 | ASSERT_EQ(tok::numeric_constant, toks[3].getKind()); |
| 225 | |
| 226 | SourceLocation defLoc = SourceMgr.translateLineCol(mainFileID, 2, 13); |
| 227 | SourceLocation loc1 = SourceMgr.translateLineCol(mainFileID, 3, 8); |
| 228 | SourceLocation loc2 = SourceMgr.translateLineCol(mainFileID, 4, 4); |
| 229 | SourceLocation loc3 = SourceMgr.translateLineCol(mainFileID, 5, 7); |
| 230 | SourceLocation defLoc2 = SourceMgr.translateLineCol(mainFileID, 6, 22); |
| 231 | defLoc = SourceMgr.getMacroArgExpandedLocation(defLoc); |
| 232 | loc1 = SourceMgr.getMacroArgExpandedLocation(loc1); |
| 233 | loc2 = SourceMgr.getMacroArgExpandedLocation(loc2); |
| 234 | loc3 = SourceMgr.getMacroArgExpandedLocation(loc3); |
| 235 | defLoc2 = SourceMgr.getMacroArgExpandedLocation(defLoc2); |
| 236 | |
| 237 | EXPECT_TRUE(defLoc.isFileID()); |
| 238 | EXPECT_TRUE(loc1.isFileID()); |
| 239 | EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc2)); |
| 240 | EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc3)); |
| 241 | EXPECT_EQ(loc2, toks[1].getLocation()); |
| 242 | EXPECT_EQ(loc3, toks[2].getLocation()); |
| 243 | EXPECT_TRUE(defLoc2.isFileID()); |
| 244 | } |
| 245 | |
Argyrios Kyrtzidis | a956450 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 246 | namespace { |
| 247 | |
| 248 | struct MacroAction { |
| 249 | SourceLocation Loc; |
| 250 | std::string Name; |
| 251 | bool isDefinition; // if false, it is expansion. |
| 252 | |
| 253 | MacroAction(SourceLocation Loc, StringRef Name, bool isDefinition) |
| 254 | : Loc(Loc), Name(Name), isDefinition(isDefinition) { } |
| 255 | }; |
| 256 | |
| 257 | class MacroTracker : public PPCallbacks { |
| 258 | std::vector<MacroAction> &Macros; |
| 259 | |
| 260 | public: |
| 261 | explicit MacroTracker(std::vector<MacroAction> &Macros) : Macros(Macros) { } |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 262 | |
| 263 | void MacroDefined(const Token &MacroNameTok, |
| 264 | const MacroDirective *MD) override { |
Argyrios Kyrtzidis | fead64b | 2013-02-24 00:05:14 +0000 | [diff] [blame] | 265 | Macros.push_back(MacroAction(MD->getLocation(), |
Argyrios Kyrtzidis | a956450 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 266 | MacroNameTok.getIdentifierInfo()->getName(), |
| 267 | true)); |
| 268 | } |
Richard Smith | 36bd40d | 2015-05-04 03:15:40 +0000 | [diff] [blame] | 269 | void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 270 | SourceRange Range, const MacroArgs *Args) override { |
Argyrios Kyrtzidis | a956450 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 271 | Macros.push_back(MacroAction(MacroNameTok.getLocation(), |
| 272 | MacroNameTok.getIdentifierInfo()->getName(), |
| 273 | false)); |
| 274 | } |
| 275 | }; |
| 276 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 277 | } |
Argyrios Kyrtzidis | a956450 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 278 | |
| 279 | TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) { |
| 280 | const char *header = |
| 281 | "#define MACRO_IN_INCLUDE 0\n"; |
| 282 | |
| 283 | const char *main = |
| 284 | "#define M(x) x\n" |
| 285 | "#define INC \"/test-header.h\"\n" |
| 286 | "#include M(INC)\n" |
| 287 | "#define INC2 </test-header.h>\n" |
| 288 | "#include M(INC2)\n"; |
| 289 | |
James Y Knight | b214cbc | 2016-03-04 19:00:41 +0000 | [diff] [blame] | 290 | std::unique_ptr<llvm::MemoryBuffer> HeaderBuf = |
| 291 | llvm::MemoryBuffer::getMemBuffer(header); |
| 292 | std::unique_ptr<llvm::MemoryBuffer> MainBuf = |
| 293 | llvm::MemoryBuffer::getMemBuffer(main); |
David Blaikie | 50a5f97 | 2014-08-29 07:59:55 +0000 | [diff] [blame] | 294 | SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(MainBuf))); |
Argyrios Kyrtzidis | a956450 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 295 | |
| 296 | const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h", |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 297 | HeaderBuf->getBufferSize(), 0); |
David Blaikie | 49cc318 | 2014-08-27 20:54:45 +0000 | [diff] [blame] | 298 | SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf)); |
Argyrios Kyrtzidis | a956450 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 299 | |
| 300 | VoidModuleLoader ModLoader; |
David Blaikie | 9c28cb3 | 2017-01-06 01:04:46 +0000 | [diff] [blame] | 301 | HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr, |
| 302 | Diags, LangOpts, &*Target); |
David Blaikie | e304168 | 2017-01-05 19:11:36 +0000 | [diff] [blame] | 303 | Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts, |
| 304 | SourceMgr, HeaderInfo, ModLoader, |
Craig Topper | 416fa34 | 2014-06-08 08:38:12 +0000 | [diff] [blame] | 305 | /*IILookup =*/nullptr, |
Alp Toker | 1ae02f6 | 2014-05-02 03:43:30 +0000 | [diff] [blame] | 306 | /*OwnsHeaderSearch =*/false); |
| 307 | PP.Initialize(*Target); |
Argyrios Kyrtzidis | a956450 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 308 | |
| 309 | std::vector<MacroAction> Macros; |
Craig Topper | b8a7053 | 2014-09-10 04:53:53 +0000 | [diff] [blame] | 310 | PP.addPPCallbacks(llvm::make_unique<MacroTracker>(Macros)); |
Argyrios Kyrtzidis | a956450 | 2012-03-27 18:47:48 +0000 | [diff] [blame] | 311 | |
| 312 | PP.EnterMainSourceFile(); |
| 313 | |
| 314 | std::vector<Token> toks; |
| 315 | while (1) { |
| 316 | Token tok; |
| 317 | PP.Lex(tok); |
| 318 | if (tok.is(tok::eof)) |
| 319 | break; |
| 320 | toks.push_back(tok); |
| 321 | } |
| 322 | |
| 323 | // Make sure we got the tokens that we expected. |
| 324 | ASSERT_EQ(0U, toks.size()); |
| 325 | |
| 326 | ASSERT_EQ(9U, Macros.size()); |
| 327 | // #define M(x) x |
| 328 | ASSERT_TRUE(Macros[0].isDefinition); |
| 329 | ASSERT_EQ("M", Macros[0].Name); |
| 330 | // #define INC "/test-header.h" |
| 331 | ASSERT_TRUE(Macros[1].isDefinition); |
| 332 | ASSERT_EQ("INC", Macros[1].Name); |
| 333 | // M expansion in #include M(INC) |
| 334 | ASSERT_FALSE(Macros[2].isDefinition); |
| 335 | ASSERT_EQ("M", Macros[2].Name); |
| 336 | // INC expansion in #include M(INC) |
| 337 | ASSERT_FALSE(Macros[3].isDefinition); |
| 338 | ASSERT_EQ("INC", Macros[3].Name); |
| 339 | // #define MACRO_IN_INCLUDE 0 |
| 340 | ASSERT_TRUE(Macros[4].isDefinition); |
| 341 | ASSERT_EQ("MACRO_IN_INCLUDE", Macros[4].Name); |
| 342 | // #define INC2 </test-header.h> |
| 343 | ASSERT_TRUE(Macros[5].isDefinition); |
| 344 | ASSERT_EQ("INC2", Macros[5].Name); |
| 345 | // M expansion in #include M(INC2) |
| 346 | ASSERT_FALSE(Macros[6].isDefinition); |
| 347 | ASSERT_EQ("M", Macros[6].Name); |
| 348 | // INC2 expansion in #include M(INC2) |
| 349 | ASSERT_FALSE(Macros[7].isDefinition); |
| 350 | ASSERT_EQ("INC2", Macros[7].Name); |
| 351 | // #define MACRO_IN_INCLUDE 0 |
| 352 | ASSERT_TRUE(Macros[8].isDefinition); |
| 353 | ASSERT_EQ("MACRO_IN_INCLUDE", Macros[8].Name); |
| 354 | |
| 355 | // The INC expansion in #include M(INC) comes before the first |
| 356 | // MACRO_IN_INCLUDE definition of the included file. |
| 357 | EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[3].Loc, Macros[4].Loc)); |
| 358 | |
| 359 | // The INC2 expansion in #include M(INC2) comes before the second |
| 360 | // MACRO_IN_INCLUDE definition of the included file. |
| 361 | EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[7].Loc, Macros[8].Loc)); |
| 362 | } |
| 363 | |
Argyrios Kyrtzidis | e841c90 | 2011-12-21 16:56:35 +0000 | [diff] [blame] | 364 | #endif |
| 365 | |
Argyrios Kyrtzidis | 2403797 | 2011-12-21 16:56:29 +0000 | [diff] [blame] | 366 | } // anonymous namespace |