blob: aa15e16b851f45f6644634c1b5e4a52f1ee97346 [file] [log] [blame]
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +00001//===- 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 Kyrtzidis24037972011-12-21 16:56:29 +000011#include "clang/Basic/Diagnostic.h"
Douglas Gregoredf8e382012-10-23 22:38:58 +000012#include "clang/Basic/DiagnosticOptions.h"
Chandler Carruth320d9662012-12-04 09:45:34 +000013#include "clang/Basic/FileManager.h"
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000014#include "clang/Basic/LangOptions.h"
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +000015#include "clang/Basic/MemoryBufferCache.h"
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000016#include "clang/Basic/TargetInfo.h"
Chandler Carruth320d9662012-12-04 09:45:34 +000017#include "clang/Basic/TargetOptions.h"
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000018#include "clang/Lex/HeaderSearch.h"
Douglas Gregor40ba1a02012-10-24 16:24:38 +000019#include "clang/Lex/HeaderSearchOptions.h"
Chandler Carruth320d9662012-12-04 09:45:34 +000020#include "clang/Lex/ModuleLoader.h"
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000021#include "clang/Lex/Preprocessor.h"
Douglas Gregor1452ff12012-10-24 17:46:57 +000022#include "clang/Lex/PreprocessorOptions.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000023#include "llvm/ADT/SmallString.h"
Alp Toker1d257e12014-06-04 03:28:55 +000024#include "llvm/Config/llvm-config.h"
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000025#include "gtest/gtest.h"
26
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000027using namespace clang;
28
29namespace {
30
31// The test fixture.
32class SourceManagerTest : public ::testing::Test {
33protected:
34 SourceManagerTest()
35 : FileMgr(FileMgrOpts),
36 DiagID(new DiagnosticIDs()),
Douglas Gregord8cfd392012-10-23 22:31:51 +000037 Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
Douglas Gregor44d63612012-10-17 00:11:35 +000038 SourceMgr(Diags, FileMgr),
39 TargetOpts(new TargetOptions) {
40 TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
Alp Toker80758082014-07-06 05:26:44 +000041 Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000042 }
43
44 FileSystemOptions FileMgrOpts;
45 FileManager FileMgr;
Dylan Noblesmithc95d8192012-02-20 14:00:23 +000046 IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000047 DiagnosticsEngine Diags;
48 SourceManager SourceMgr;
49 LangOptions LangOpts;
Alp Toker80758082014-07-06 05:26:44 +000050 std::shared_ptr<TargetOptions> TargetOpts;
Dylan Noblesmithc95d8192012-02-20 14:00:23 +000051 IntrusiveRefCntPtr<TargetInfo> Target;
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000052};
53
54class VoidModuleLoader : public ModuleLoader {
John Thompson2d94bbb2014-04-23 19:04:32 +000055 ModuleLoadResult loadModule(SourceLocation ImportLoc,
56 ModuleIdPath Path,
57 Module::NameVisibilityKind Visibility,
58 bool IsInclusionDirective) override {
Douglas Gregor8c058932012-11-30 00:01:57 +000059 return ModuleLoadResult();
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000060 }
NAKAMURA Takumie73d2a92013-01-12 02:16:29 +000061
John Thompson2d94bbb2014-04-23 19:04:32 +000062 void makeModuleVisible(Module *Mod,
63 Module::NameVisibilityKind Visibility,
Richard Smitha7e2cc62015-05-01 01:53:09 +000064 SourceLocation ImportLoc) override { }
John Thompson2255f2c2014-04-23 12:57:01 +000065
John Thompson2d94bbb2014-04-23 19:04:32 +000066 GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
Craig Topper416fa342014-06-08 08:38:12 +000067 { return nullptr; }
John Thompson2d94bbb2014-04-23 19:04:32 +000068 bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
Hans Wennborg4afe5042015-07-22 20:46:26 +000069 { return 0; }
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000070};
71
72TEST_F(SourceManagerTest, isBeforeInTranslationUnit) {
73 const char *source =
74 "#define M(x) [x]\n"
75 "M(foo)";
James Y Knightb214cbc2016-03-04 19:00:41 +000076 std::unique_ptr<llvm::MemoryBuffer> Buf =
77 llvm::MemoryBuffer::getMemBuffer(source);
David Blaikie50a5f972014-08-29 07:59:55 +000078 FileID mainFileID = SourceMgr.createFileID(std::move(Buf));
Alp Tokerb671e342014-05-21 01:12:41 +000079 SourceMgr.setMainFileID(mainFileID);
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000080
81 VoidModuleLoader ModLoader;
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +000082 MemoryBufferCache PCMCache;
David Blaikie9c28cb32017-01-06 01:04:46 +000083 HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
84 Diags, LangOpts, &*Target);
David Blaikiee3041682017-01-05 19:11:36 +000085 Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +000086 SourceMgr, PCMCache, HeaderInfo, ModLoader,
Craig Topper416fa342014-06-08 08:38:12 +000087 /*IILookup =*/nullptr,
Alp Toker1ae02f62014-05-02 03:43:30 +000088 /*OwnsHeaderSearch =*/false);
89 PP.Initialize(*Target);
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000090 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 Noblesmithf1a13f22012-02-13 12:32:26 +0000116 SmallString<32> str;
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +0000117 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 Rose8d63d5b2012-06-19 03:09:38 +0000126TEST_F(SourceManagerTest, getColumnNumber) {
127 const char *Source =
128 "int x;\n"
129 "int y;";
130
James Y Knightb214cbc2016-03-04 19:00:41 +0000131 std::unique_ptr<llvm::MemoryBuffer> Buf =
132 llvm::MemoryBuffer::getMemBuffer(Source);
David Blaikie50a5f972014-08-29 07:59:55 +0000133 FileID MainFileID = SourceMgr.createFileID(std::move(Buf));
Alp Tokerb671e342014-05-21 01:12:41 +0000134 SourceMgr.setMainFileID(MainFileID);
Jordan Rose8d63d5b2012-06-19 03:09:38 +0000135
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 Topper416fa342014-06-08 08:38:12 +0000173 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, nullptr));
Jordan Rose8d63d5b2012-06-19 03:09:38 +0000174}
175
Argyrios Kyrtzidise841c902011-12-21 16:56:35 +0000176#if defined(LLVM_ON_UNIX)
177
178TEST_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 Knightb214cbc2016-03-04 19:00:41 +0000191 std::unique_ptr<llvm::MemoryBuffer> HeaderBuf =
192 llvm::MemoryBuffer::getMemBuffer(header);
193 std::unique_ptr<llvm::MemoryBuffer> MainBuf =
194 llvm::MemoryBuffer::getMemBuffer(main);
David Blaikie50a5f972014-08-29 07:59:55 +0000195 FileID mainFileID = SourceMgr.createFileID(std::move(MainBuf));
Alp Tokerb671e342014-05-21 01:12:41 +0000196 SourceMgr.setMainFileID(mainFileID);
Argyrios Kyrtzidise841c902011-12-21 16:56:35 +0000197
198 const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
Rafael Espindolad87f8d72014-08-27 20:03:29 +0000199 HeaderBuf->getBufferSize(), 0);
David Blaikie49cc3182014-08-27 20:54:45 +0000200 SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
Argyrios Kyrtzidise841c902011-12-21 16:56:35 +0000201
202 VoidModuleLoader ModLoader;
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +0000203 MemoryBufferCache PCMCache;
David Blaikie9c28cb32017-01-06 01:04:46 +0000204 HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
205 Diags, LangOpts, &*Target);
David Blaikiee3041682017-01-05 19:11:36 +0000206 Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +0000207 SourceMgr, PCMCache, HeaderInfo, ModLoader,
Craig Topper416fa342014-06-08 08:38:12 +0000208 /*IILookup =*/nullptr,
Alp Toker1ae02f62014-05-02 03:43:30 +0000209 /*OwnsHeaderSearch =*/false);
210 PP.Initialize(*Target);
Argyrios Kyrtzidise841c902011-12-21 16:56:35 +0000211 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 Kyrtzidisa9564502012-03-27 18:47:48 +0000249namespace {
250
251struct MacroAction {
Vedant Kumar349a6242017-04-26 21:05:44 +0000252 enum Kind { kExpansion, kDefinition, kUnDefinition};
253
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000254 SourceLocation Loc;
255 std::string Name;
Vedant Kumar349a6242017-04-26 21:05:44 +0000256 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 Kyrtzidisa9564502012-03-27 18:47:48 +0000264};
265
266class MacroTracker : public PPCallbacks {
267 std::vector<MacroAction> &Macros;
268
269public:
270 explicit MacroTracker(std::vector<MacroAction> &Macros) : Macros(Macros) { }
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000271
272 void MacroDefined(const Token &MacroNameTok,
273 const MacroDirective *MD) override {
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000274 Macros.push_back(MacroAction(MD->getLocation(),
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000275 MacroNameTok.getIdentifierInfo()->getName(),
Vedant Kumar349a6242017-04-26 21:05:44 +0000276 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 Kyrtzidisa9564502012-03-27 18:47:48 +0000286 }
Richard Smith36bd40d2015-05-04 03:15:40 +0000287 void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000288 SourceRange Range, const MacroArgs *Args) override {
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000289 Macros.push_back(MacroAction(MacroNameTok.getLocation(),
290 MacroNameTok.getIdentifierInfo()->getName(),
Vedant Kumar349a6242017-04-26 21:05:44 +0000291 MacroAction::kExpansion));
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000292 }
293};
294
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000295}
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000296
297TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) {
298 const char *header =
Vedant Kumar349a6242017-04-26 21:05:44 +0000299 "#define MACRO_IN_INCLUDE 0\n"
300 "#define MACRO_DEFINED\n"
301 "#undef MACRO_DEFINED\n"
302 "#undef MACRO_UNDEFINED\n";
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000303
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 Knightb214cbc2016-03-04 19:00:41 +0000311 std::unique_ptr<llvm::MemoryBuffer> HeaderBuf =
312 llvm::MemoryBuffer::getMemBuffer(header);
313 std::unique_ptr<llvm::MemoryBuffer> MainBuf =
314 llvm::MemoryBuffer::getMemBuffer(main);
David Blaikie50a5f972014-08-29 07:59:55 +0000315 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(MainBuf)));
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000316
317 const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
Rafael Espindolad87f8d72014-08-27 20:03:29 +0000318 HeaderBuf->getBufferSize(), 0);
David Blaikie49cc3182014-08-27 20:54:45 +0000319 SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000320
321 VoidModuleLoader ModLoader;
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +0000322 MemoryBufferCache PCMCache;
David Blaikie9c28cb32017-01-06 01:04:46 +0000323 HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
324 Diags, LangOpts, &*Target);
David Blaikiee3041682017-01-05 19:11:36 +0000325 Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +0000326 SourceMgr, PCMCache, HeaderInfo, ModLoader,
Craig Topper416fa342014-06-08 08:38:12 +0000327 /*IILookup =*/nullptr,
Alp Toker1ae02f62014-05-02 03:43:30 +0000328 /*OwnsHeaderSearch =*/false);
329 PP.Initialize(*Target);
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000330
331 std::vector<MacroAction> Macros;
Craig Topperb8a70532014-09-10 04:53:53 +0000332 PP.addPPCallbacks(llvm::make_unique<MacroTracker>(Macros));
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000333
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 Kumar349a6242017-04-26 21:05:44 +0000348 ASSERT_EQ(15U, Macros.size());
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000349 // #define M(x) x
Vedant Kumar349a6242017-04-26 21:05:44 +0000350 ASSERT_TRUE(Macros[0].isDefinition());
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000351 ASSERT_EQ("M", Macros[0].Name);
352 // #define INC "/test-header.h"
Vedant Kumar349a6242017-04-26 21:05:44 +0000353 ASSERT_TRUE(Macros[1].isDefinition());
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000354 ASSERT_EQ("INC", Macros[1].Name);
355 // M expansion in #include M(INC)
Vedant Kumar349a6242017-04-26 21:05:44 +0000356 ASSERT_FALSE(Macros[2].isDefinition());
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000357 ASSERT_EQ("M", Macros[2].Name);
358 // INC expansion in #include M(INC)
Vedant Kumar349a6242017-04-26 21:05:44 +0000359 ASSERT_TRUE(Macros[3].isExpansion());
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000360 ASSERT_EQ("INC", Macros[3].Name);
361 // #define MACRO_IN_INCLUDE 0
Vedant Kumar349a6242017-04-26 21:05:44 +0000362 ASSERT_TRUE(Macros[4].isDefinition());
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000363 ASSERT_EQ("MACRO_IN_INCLUDE", Macros[4].Name);
Vedant Kumar349a6242017-04-26 21:05:44 +0000364 // #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 Kyrtzidisa9564502012-03-27 18:47:48 +0000376 // #define INC2 </test-header.h>
Vedant Kumar349a6242017-04-26 21:05:44 +0000377 ASSERT_TRUE(Macros[8].isDefinition());
378 ASSERT_EQ("INC2", Macros[8].Name);
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000379 // M expansion in #include M(INC2)
Vedant Kumar349a6242017-04-26 21:05:44 +0000380 ASSERT_FALSE(Macros[9].isDefinition());
381 ASSERT_EQ("M", Macros[9].Name);
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000382 // INC2 expansion in #include M(INC2)
Vedant Kumar349a6242017-04-26 21:05:44 +0000383 ASSERT_TRUE(Macros[10].isExpansion());
384 ASSERT_EQ("INC2", Macros[10].Name);
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000385 // #define MACRO_IN_INCLUDE 0
Vedant Kumar349a6242017-04-26 21:05:44 +0000386 ASSERT_TRUE(Macros[11].isDefinition());
387 ASSERT_EQ("MACRO_IN_INCLUDE", Macros[11].Name);
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000388
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 Kumar349a6242017-04-26 21:05:44 +0000395 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[10].Loc, Macros[11].Loc));
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000396}
397
Argyrios Kyrtzidise841c902011-12-21 16:56:35 +0000398#endif
399
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +0000400} // anonymous namespace