blob: 9ea093c6b2b87d2cc2c45f59339e72cc4878a12f [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"
Alp Toker1d257e12014-06-04 03:28:55 +000023#include "llvm/Config/llvm-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";
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,
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
Craig Topper416fa342014-06-08 08:38:12 +000068 { return nullptr; }
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);
Alp Tokerb671e342014-05-21 01:12:41 +000078 FileID mainFileID = SourceMgr.createFileID(buf);
79 SourceMgr.setMainFileID(mainFileID);
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000080
81 VoidModuleLoader ModLoader;
Manuel Klimek1f76c4e2013-10-24 07:51:24 +000082 HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts,
Douglas Gregor40ba1a02012-10-24 16:24:38 +000083 &*Target);
Alp Toker96637802014-05-02 03:43:38 +000084 Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, SourceMgr,
85 HeaderInfo, ModLoader,
Craig Topper416fa342014-06-08 08:38:12 +000086 /*IILookup =*/nullptr,
Alp Toker1ae02f62014-05-02 03:43:30 +000087 /*OwnsHeaderSearch =*/false);
88 PP.Initialize(*Target);
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000089 PP.EnterMainSourceFile();
90
91 std::vector<Token> toks;
92 while (1) {
93 Token tok;
94 PP.Lex(tok);
95 if (tok.is(tok::eof))
96 break;
97 toks.push_back(tok);
98 }
99
100 // Make sure we got the tokens that we expected.
101 ASSERT_EQ(3U, toks.size());
102 ASSERT_EQ(tok::l_square, toks[0].getKind());
103 ASSERT_EQ(tok::identifier, toks[1].getKind());
104 ASSERT_EQ(tok::r_square, toks[2].getKind());
105
106 SourceLocation lsqrLoc = toks[0].getLocation();
107 SourceLocation idLoc = toks[1].getLocation();
108 SourceLocation rsqrLoc = toks[2].getLocation();
109
110 SourceLocation macroExpStartLoc = SourceMgr.translateLineCol(mainFileID, 2, 1);
111 SourceLocation macroExpEndLoc = SourceMgr.translateLineCol(mainFileID, 2, 6);
112 ASSERT_TRUE(macroExpStartLoc.isFileID());
113 ASSERT_TRUE(macroExpEndLoc.isFileID());
114
Dylan Noblesmithf1a13f22012-02-13 12:32:26 +0000115 SmallString<32> str;
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +0000116 ASSERT_EQ("M", PP.getSpelling(macroExpStartLoc, str));
117 ASSERT_EQ(")", PP.getSpelling(macroExpEndLoc, str));
118
119 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(lsqrLoc, idLoc));
120 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, rsqrLoc));
121 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(macroExpStartLoc, idLoc));
122 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, macroExpEndLoc));
123}
124
Jordan Rose8d63d5b2012-06-19 03:09:38 +0000125TEST_F(SourceManagerTest, getColumnNumber) {
126 const char *Source =
127 "int x;\n"
128 "int y;";
129
130 MemoryBuffer *Buf = MemoryBuffer::getMemBuffer(Source);
Alp Tokerb671e342014-05-21 01:12:41 +0000131 FileID MainFileID = SourceMgr.createFileID(Buf);
132 SourceMgr.setMainFileID(MainFileID);
Jordan Rose8d63d5b2012-06-19 03:09:38 +0000133
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 Topper416fa342014-06-08 08:38:12 +0000171 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, nullptr));
Jordan Rose8d63d5b2012-06-19 03:09:38 +0000172}
173
Argyrios Kyrtzidise841c902011-12-21 16:56:35 +0000174#if defined(LLVM_ON_UNIX)
175
176TEST_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
189 MemoryBuffer *headerBuf = MemoryBuffer::getMemBuffer(header);
190 MemoryBuffer *mainBuf = MemoryBuffer::getMemBuffer(main);
Alp Tokerb671e342014-05-21 01:12:41 +0000191 FileID mainFileID = SourceMgr.createFileID(mainBuf);
192 SourceMgr.setMainFileID(mainFileID);
Argyrios Kyrtzidise841c902011-12-21 16:56:35 +0000193
194 const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
195 headerBuf->getBufferSize(), 0);
196 SourceMgr.overrideFileContents(headerFile, headerBuf);
197
198 VoidModuleLoader ModLoader;
Manuel Klimek1f76c4e2013-10-24 07:51:24 +0000199 HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts,
Douglas Gregor40ba1a02012-10-24 16:24:38 +0000200 &*Target);
Alp Toker96637802014-05-02 03:43:38 +0000201 Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, SourceMgr,
202 HeaderInfo, ModLoader,
Craig Topper416fa342014-06-08 08:38:12 +0000203 /*IILookup =*/nullptr,
Alp Toker1ae02f62014-05-02 03:43:30 +0000204 /*OwnsHeaderSearch =*/false);
205 PP.Initialize(*Target);
Argyrios Kyrtzidise841c902011-12-21 16:56:35 +0000206 PP.EnterMainSourceFile();
207
208 std::vector<Token> toks;
209 while (1) {
210 Token tok;
211 PP.Lex(tok);
212 if (tok.is(tok::eof))
213 break;
214 toks.push_back(tok);
215 }
216
217 // Make sure we got the tokens that we expected.
218 ASSERT_EQ(4U, toks.size());
219 ASSERT_EQ(tok::numeric_constant, toks[0].getKind());
220 ASSERT_EQ(tok::numeric_constant, toks[1].getKind());
221 ASSERT_EQ(tok::numeric_constant, toks[2].getKind());
222 ASSERT_EQ(tok::numeric_constant, toks[3].getKind());
223
224 SourceLocation defLoc = SourceMgr.translateLineCol(mainFileID, 2, 13);
225 SourceLocation loc1 = SourceMgr.translateLineCol(mainFileID, 3, 8);
226 SourceLocation loc2 = SourceMgr.translateLineCol(mainFileID, 4, 4);
227 SourceLocation loc3 = SourceMgr.translateLineCol(mainFileID, 5, 7);
228 SourceLocation defLoc2 = SourceMgr.translateLineCol(mainFileID, 6, 22);
229 defLoc = SourceMgr.getMacroArgExpandedLocation(defLoc);
230 loc1 = SourceMgr.getMacroArgExpandedLocation(loc1);
231 loc2 = SourceMgr.getMacroArgExpandedLocation(loc2);
232 loc3 = SourceMgr.getMacroArgExpandedLocation(loc3);
233 defLoc2 = SourceMgr.getMacroArgExpandedLocation(defLoc2);
234
235 EXPECT_TRUE(defLoc.isFileID());
236 EXPECT_TRUE(loc1.isFileID());
237 EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc2));
238 EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc3));
239 EXPECT_EQ(loc2, toks[1].getLocation());
240 EXPECT_EQ(loc3, toks[2].getLocation());
241 EXPECT_TRUE(defLoc2.isFileID());
242}
243
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000244namespace {
245
246struct MacroAction {
247 SourceLocation Loc;
248 std::string Name;
249 bool isDefinition; // if false, it is expansion.
250
251 MacroAction(SourceLocation Loc, StringRef Name, bool isDefinition)
252 : Loc(Loc), Name(Name), isDefinition(isDefinition) { }
253};
254
255class MacroTracker : public PPCallbacks {
256 std::vector<MacroAction> &Macros;
257
258public:
259 explicit MacroTracker(std::vector<MacroAction> &Macros) : Macros(Macros) { }
260
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000261 virtual void MacroDefined(const Token &MacroNameTok,
262 const MacroDirective *MD) {
263 Macros.push_back(MacroAction(MD->getLocation(),
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000264 MacroNameTok.getIdentifierInfo()->getName(),
265 true));
266 }
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000267 virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
Argyrios Kyrtzidis37e48ff2013-05-03 22:31:32 +0000268 SourceRange Range, const MacroArgs *Args) {
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000269 Macros.push_back(MacroAction(MacroNameTok.getLocation(),
270 MacroNameTok.getIdentifierInfo()->getName(),
271 false));
272 }
273};
274
275}
276
277TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) {
278 const char *header =
279 "#define MACRO_IN_INCLUDE 0\n";
280
281 const char *main =
282 "#define M(x) x\n"
283 "#define INC \"/test-header.h\"\n"
284 "#include M(INC)\n"
285 "#define INC2 </test-header.h>\n"
286 "#include M(INC2)\n";
287
288 MemoryBuffer *headerBuf = MemoryBuffer::getMemBuffer(header);
289 MemoryBuffer *mainBuf = MemoryBuffer::getMemBuffer(main);
Alp Tokerb671e342014-05-21 01:12:41 +0000290 SourceMgr.setMainFileID(SourceMgr.createFileID(mainBuf));
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000291
292 const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
293 headerBuf->getBufferSize(), 0);
294 SourceMgr.overrideFileContents(headerFile, headerBuf);
295
296 VoidModuleLoader ModLoader;
Manuel Klimek1f76c4e2013-10-24 07:51:24 +0000297 HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts,
Douglas Gregor40ba1a02012-10-24 16:24:38 +0000298 &*Target);
Alp Toker96637802014-05-02 03:43:38 +0000299 Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, SourceMgr,
300 HeaderInfo, ModLoader,
Craig Topper416fa342014-06-08 08:38:12 +0000301 /*IILookup =*/nullptr,
Alp Toker1ae02f62014-05-02 03:43:30 +0000302 /*OwnsHeaderSearch =*/false);
303 PP.Initialize(*Target);
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000304
305 std::vector<MacroAction> Macros;
306 PP.addPPCallbacks(new MacroTracker(Macros));
307
308 PP.EnterMainSourceFile();
309
310 std::vector<Token> toks;
311 while (1) {
312 Token tok;
313 PP.Lex(tok);
314 if (tok.is(tok::eof))
315 break;
316 toks.push_back(tok);
317 }
318
319 // Make sure we got the tokens that we expected.
320 ASSERT_EQ(0U, toks.size());
321
322 ASSERT_EQ(9U, Macros.size());
323 // #define M(x) x
324 ASSERT_TRUE(Macros[0].isDefinition);
325 ASSERT_EQ("M", Macros[0].Name);
326 // #define INC "/test-header.h"
327 ASSERT_TRUE(Macros[1].isDefinition);
328 ASSERT_EQ("INC", Macros[1].Name);
329 // M expansion in #include M(INC)
330 ASSERT_FALSE(Macros[2].isDefinition);
331 ASSERT_EQ("M", Macros[2].Name);
332 // INC expansion in #include M(INC)
333 ASSERT_FALSE(Macros[3].isDefinition);
334 ASSERT_EQ("INC", Macros[3].Name);
335 // #define MACRO_IN_INCLUDE 0
336 ASSERT_TRUE(Macros[4].isDefinition);
337 ASSERT_EQ("MACRO_IN_INCLUDE", Macros[4].Name);
338 // #define INC2 </test-header.h>
339 ASSERT_TRUE(Macros[5].isDefinition);
340 ASSERT_EQ("INC2", Macros[5].Name);
341 // M expansion in #include M(INC2)
342 ASSERT_FALSE(Macros[6].isDefinition);
343 ASSERT_EQ("M", Macros[6].Name);
344 // INC2 expansion in #include M(INC2)
345 ASSERT_FALSE(Macros[7].isDefinition);
346 ASSERT_EQ("INC2", Macros[7].Name);
347 // #define MACRO_IN_INCLUDE 0
348 ASSERT_TRUE(Macros[8].isDefinition);
349 ASSERT_EQ("MACRO_IN_INCLUDE", Macros[8].Name);
350
351 // The INC expansion in #include M(INC) comes before the first
352 // MACRO_IN_INCLUDE definition of the included file.
353 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[3].Loc, Macros[4].Loc));
354
355 // The INC2 expansion in #include M(INC2) comes before the second
356 // MACRO_IN_INCLUDE definition of the included file.
357 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[7].Loc, Macros[8].Loc));
358}
359
Argyrios Kyrtzidise841c902011-12-21 16:56:35 +0000360#endif
361
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +0000362} // anonymous namespace