blob: 36e6a5578bcd5dc33230402a56ef4a583d17dfbf [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"
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000015#include "clang/Basic/TargetInfo.h"
Chandler Carruth320d9662012-12-04 09:45:34 +000016#include "clang/Basic/TargetOptions.h"
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000017#include "clang/Lex/HeaderSearch.h"
Douglas Gregor40ba1a02012-10-24 16:24:38 +000018#include "clang/Lex/HeaderSearchOptions.h"
Chandler Carruth320d9662012-12-04 09:45:34 +000019#include "clang/Lex/ModuleLoader.h"
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000020#include "clang/Lex/Preprocessor.h"
Douglas Gregor1452ff12012-10-24 17:46:57 +000021#include "clang/Lex/PreprocessorOptions.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000022#include "llvm/ADT/SmallString.h"
Argyrios Kyrtzidise841c902011-12-21 16:56:35 +000023#include "llvm/Config/config.h"
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000024#include "gtest/gtest.h"
25
26using namespace llvm;
27using 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";
NAKAMURA Takumife40a352012-11-16 04:40:11 +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;
Douglas Gregor44d63612012-10-17 00:11:35 +000050 IntrusiveRefCntPtr<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,
64 SourceLocation ImportLoc,
65 bool Complain) override { }
John Thompson2255f2c2014-04-23 12:57:01 +000066
John Thompson2d94bbb2014-04-23 19:04:32 +000067 GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
John Thompson2255f2c2014-04-23 12:57:01 +000068 { return 0; }
John Thompson2d94bbb2014-04-23 19:04:32 +000069 bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
70 { return 0; };
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000071};
72
73TEST_F(SourceManagerTest, isBeforeInTranslationUnit) {
74 const char *source =
75 "#define M(x) [x]\n"
76 "M(foo)";
77 MemoryBuffer *buf = MemoryBuffer::getMemBuffer(source);
78 FileID mainFileID = SourceMgr.createMainFileIDForMemBuffer(buf);
79
80 VoidModuleLoader ModLoader;
Manuel Klimek1f76c4e2013-10-24 07:51:24 +000081 HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts,
Douglas Gregor40ba1a02012-10-24 16:24:38 +000082 &*Target);
Alp Toker96637802014-05-02 03:43:38 +000083 Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, SourceMgr,
84 HeaderInfo, ModLoader,
85 /*IILookup =*/0,
Alp Toker1ae02f62014-05-02 03:43:30 +000086 /*OwnsHeaderSearch =*/false);
87 PP.Initialize(*Target);
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000088 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 Noblesmithf1a13f22012-02-13 12:32:26 +0000114 SmallString<32> str;
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +0000115 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 Rose8d63d5b2012-06-19 03:09:38 +0000124TEST_F(SourceManagerTest, getColumnNumber) {
125 const char *Source =
126 "int x;\n"
127 "int y;";
128
129 MemoryBuffer *Buf = MemoryBuffer::getMemBuffer(Source);
130 FileID MainFileID = SourceMgr.createMainFileIDForMemBuffer(Buf);
131
132 bool Invalid;
133
134 Invalid = false;
135 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, &Invalid));
136 EXPECT_TRUE(!Invalid);
137
138 Invalid = false;
139 EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 4, &Invalid));
140 EXPECT_TRUE(!Invalid);
141
142 Invalid = false;
143 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 7, &Invalid));
144 EXPECT_TRUE(!Invalid);
145
146 Invalid = false;
147 EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 11, &Invalid));
148 EXPECT_TRUE(!Invalid);
149
150 Invalid = false;
151 EXPECT_EQ(7U, SourceMgr.getColumnNumber(MainFileID, strlen(Source),
152 &Invalid));
153 EXPECT_TRUE(!Invalid);
154
155 Invalid = false;
156 SourceMgr.getColumnNumber(MainFileID, strlen(Source)+1, &Invalid);
157 EXPECT_TRUE(Invalid);
158
159 // Test invalid files
160 Invalid = false;
161 SourceMgr.getColumnNumber(FileID(), 0, &Invalid);
162 EXPECT_TRUE(Invalid);
163
164 Invalid = false;
165 SourceMgr.getColumnNumber(FileID(), 1, &Invalid);
166 EXPECT_TRUE(Invalid);
167
168 // Test with no invalid flag.
169 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, NULL));
170}
171
Argyrios Kyrtzidise841c902011-12-21 16:56:35 +0000172#if defined(LLVM_ON_UNIX)
173
174TEST_F(SourceManagerTest, getMacroArgExpandedLocation) {
175 const char *header =
176 "#define FM(x,y) x\n";
177
178 const char *main =
179 "#include \"/test-header.h\"\n"
180 "#define VAL 0\n"
181 "FM(VAL,0)\n"
182 "FM(0,VAL)\n"
183 "FM(FM(0,VAL),0)\n"
184 "#define CONCAT(X, Y) X##Y\n"
185 "CONCAT(1,1)\n";
186
187 MemoryBuffer *headerBuf = MemoryBuffer::getMemBuffer(header);
188 MemoryBuffer *mainBuf = MemoryBuffer::getMemBuffer(main);
189 FileID mainFileID = SourceMgr.createMainFileIDForMemBuffer(mainBuf);
190
191 const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
192 headerBuf->getBufferSize(), 0);
193 SourceMgr.overrideFileContents(headerFile, headerBuf);
194
195 VoidModuleLoader ModLoader;
Manuel Klimek1f76c4e2013-10-24 07:51:24 +0000196 HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts,
Douglas Gregor40ba1a02012-10-24 16:24:38 +0000197 &*Target);
Alp Toker96637802014-05-02 03:43:38 +0000198 Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, SourceMgr,
199 HeaderInfo, ModLoader,
200 /*IILookup =*/0,
Alp Toker1ae02f62014-05-02 03:43:30 +0000201 /*OwnsHeaderSearch =*/false);
202 PP.Initialize(*Target);
Argyrios Kyrtzidise841c902011-12-21 16:56:35 +0000203 PP.EnterMainSourceFile();
204
205 std::vector<Token> toks;
206 while (1) {
207 Token tok;
208 PP.Lex(tok);
209 if (tok.is(tok::eof))
210 break;
211 toks.push_back(tok);
212 }
213
214 // Make sure we got the tokens that we expected.
215 ASSERT_EQ(4U, toks.size());
216 ASSERT_EQ(tok::numeric_constant, toks[0].getKind());
217 ASSERT_EQ(tok::numeric_constant, toks[1].getKind());
218 ASSERT_EQ(tok::numeric_constant, toks[2].getKind());
219 ASSERT_EQ(tok::numeric_constant, toks[3].getKind());
220
221 SourceLocation defLoc = SourceMgr.translateLineCol(mainFileID, 2, 13);
222 SourceLocation loc1 = SourceMgr.translateLineCol(mainFileID, 3, 8);
223 SourceLocation loc2 = SourceMgr.translateLineCol(mainFileID, 4, 4);
224 SourceLocation loc3 = SourceMgr.translateLineCol(mainFileID, 5, 7);
225 SourceLocation defLoc2 = SourceMgr.translateLineCol(mainFileID, 6, 22);
226 defLoc = SourceMgr.getMacroArgExpandedLocation(defLoc);
227 loc1 = SourceMgr.getMacroArgExpandedLocation(loc1);
228 loc2 = SourceMgr.getMacroArgExpandedLocation(loc2);
229 loc3 = SourceMgr.getMacroArgExpandedLocation(loc3);
230 defLoc2 = SourceMgr.getMacroArgExpandedLocation(defLoc2);
231
232 EXPECT_TRUE(defLoc.isFileID());
233 EXPECT_TRUE(loc1.isFileID());
234 EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc2));
235 EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc3));
236 EXPECT_EQ(loc2, toks[1].getLocation());
237 EXPECT_EQ(loc3, toks[2].getLocation());
238 EXPECT_TRUE(defLoc2.isFileID());
239}
240
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000241namespace {
242
243struct MacroAction {
244 SourceLocation Loc;
245 std::string Name;
246 bool isDefinition; // if false, it is expansion.
247
248 MacroAction(SourceLocation Loc, StringRef Name, bool isDefinition)
249 : Loc(Loc), Name(Name), isDefinition(isDefinition) { }
250};
251
252class MacroTracker : public PPCallbacks {
253 std::vector<MacroAction> &Macros;
254
255public:
256 explicit MacroTracker(std::vector<MacroAction> &Macros) : Macros(Macros) { }
257
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000258 virtual void MacroDefined(const Token &MacroNameTok,
259 const MacroDirective *MD) {
260 Macros.push_back(MacroAction(MD->getLocation(),
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000261 MacroNameTok.getIdentifierInfo()->getName(),
262 true));
263 }
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000264 virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
Argyrios Kyrtzidis37e48ff2013-05-03 22:31:32 +0000265 SourceRange Range, const MacroArgs *Args) {
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000266 Macros.push_back(MacroAction(MacroNameTok.getLocation(),
267 MacroNameTok.getIdentifierInfo()->getName(),
268 false));
269 }
270};
271
272}
273
274TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) {
275 const char *header =
276 "#define MACRO_IN_INCLUDE 0\n";
277
278 const char *main =
279 "#define M(x) x\n"
280 "#define INC \"/test-header.h\"\n"
281 "#include M(INC)\n"
282 "#define INC2 </test-header.h>\n"
283 "#include M(INC2)\n";
284
285 MemoryBuffer *headerBuf = MemoryBuffer::getMemBuffer(header);
286 MemoryBuffer *mainBuf = MemoryBuffer::getMemBuffer(main);
287 SourceMgr.createMainFileIDForMemBuffer(mainBuf);
288
289 const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
290 headerBuf->getBufferSize(), 0);
291 SourceMgr.overrideFileContents(headerFile, headerBuf);
292
293 VoidModuleLoader ModLoader;
Manuel Klimek1f76c4e2013-10-24 07:51:24 +0000294 HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts,
Douglas Gregor40ba1a02012-10-24 16:24:38 +0000295 &*Target);
Alp Toker96637802014-05-02 03:43:38 +0000296 Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, SourceMgr,
297 HeaderInfo, ModLoader,
298 /*IILookup =*/0,
Alp Toker1ae02f62014-05-02 03:43:30 +0000299 /*OwnsHeaderSearch =*/false);
300 PP.Initialize(*Target);
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000301
302 std::vector<MacroAction> Macros;
303 PP.addPPCallbacks(new MacroTracker(Macros));
304
305 PP.EnterMainSourceFile();
306
307 std::vector<Token> toks;
308 while (1) {
309 Token tok;
310 PP.Lex(tok);
311 if (tok.is(tok::eof))
312 break;
313 toks.push_back(tok);
314 }
315
316 // Make sure we got the tokens that we expected.
317 ASSERT_EQ(0U, toks.size());
318
319 ASSERT_EQ(9U, Macros.size());
320 // #define M(x) x
321 ASSERT_TRUE(Macros[0].isDefinition);
322 ASSERT_EQ("M", Macros[0].Name);
323 // #define INC "/test-header.h"
324 ASSERT_TRUE(Macros[1].isDefinition);
325 ASSERT_EQ("INC", Macros[1].Name);
326 // M expansion in #include M(INC)
327 ASSERT_FALSE(Macros[2].isDefinition);
328 ASSERT_EQ("M", Macros[2].Name);
329 // INC expansion in #include M(INC)
330 ASSERT_FALSE(Macros[3].isDefinition);
331 ASSERT_EQ("INC", Macros[3].Name);
332 // #define MACRO_IN_INCLUDE 0
333 ASSERT_TRUE(Macros[4].isDefinition);
334 ASSERT_EQ("MACRO_IN_INCLUDE", Macros[4].Name);
335 // #define INC2 </test-header.h>
336 ASSERT_TRUE(Macros[5].isDefinition);
337 ASSERT_EQ("INC2", Macros[5].Name);
338 // M expansion in #include M(INC2)
339 ASSERT_FALSE(Macros[6].isDefinition);
340 ASSERT_EQ("M", Macros[6].Name);
341 // INC2 expansion in #include M(INC2)
342 ASSERT_FALSE(Macros[7].isDefinition);
343 ASSERT_EQ("INC2", Macros[7].Name);
344 // #define MACRO_IN_INCLUDE 0
345 ASSERT_TRUE(Macros[8].isDefinition);
346 ASSERT_EQ("MACRO_IN_INCLUDE", Macros[8].Name);
347
348 // The INC expansion in #include M(INC) comes before the first
349 // MACRO_IN_INCLUDE definition of the included file.
350 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[3].Loc, Macros[4].Loc));
351
352 // The INC2 expansion in #include M(INC2) comes before the second
353 // MACRO_IN_INCLUDE definition of the included file.
354 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[7].Loc, Macros[8].Loc));
355}
356
Argyrios Kyrtzidise841c902011-12-21 16:56:35 +0000357#endif
358
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +0000359} // anonymous namespace