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