blob: 130ea0a5a87d7857cdf2b0a0cfd2351e8b045b62 [file] [log] [blame]
Argyrios Kyrtzidisd7711ec2011-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 Kyrtzidisd7711ec2011-12-21 16:56:29 +000011#include "clang/Basic/Diagnostic.h"
Douglas Gregor3aeb34f2012-10-23 22:38:58 +000012#include "clang/Basic/DiagnosticOptions.h"
Chandler Carruth1050e8b2012-12-04 09:45:34 +000013#include "clang/Basic/FileManager.h"
Argyrios Kyrtzidisd7711ec2011-12-21 16:56:29 +000014#include "clang/Basic/LangOptions.h"
Argyrios Kyrtzidisd7711ec2011-12-21 16:56:29 +000015#include "clang/Basic/TargetInfo.h"
Chandler Carruth1050e8b2012-12-04 09:45:34 +000016#include "clang/Basic/TargetOptions.h"
Argyrios Kyrtzidisd7711ec2011-12-21 16:56:29 +000017#include "clang/Lex/HeaderSearch.h"
Douglas Gregorb0985c82012-10-24 16:24:38 +000018#include "clang/Lex/HeaderSearchOptions.h"
Chandler Carruth1050e8b2012-12-04 09:45:34 +000019#include "clang/Lex/ModuleLoader.h"
Argyrios Kyrtzidisd7711ec2011-12-21 16:56:29 +000020#include "clang/Lex/Preprocessor.h"
Douglas Gregor36a16492012-10-24 17:46:57 +000021#include "clang/Lex/PreprocessorOptions.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000022#include "llvm/ADT/SmallString.h"
Argyrios Kyrtzidiscee5ec92011-12-21 16:56:35 +000023#include "llvm/Config/config.h"
Argyrios Kyrtzidisd7711ec2011-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 Gregor8e023612012-10-23 22:31:51 +000037 Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
Douglas Gregor07f8cf42012-10-17 00:11:35 +000038 SourceMgr(Diags, FileMgr),
39 TargetOpts(new TargetOptions) {
40 TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
NAKAMURA Takumi3a4c8cf2012-11-16 04:40:11 +000041 Target = TargetInfo::CreateTargetInfo(Diags, &*TargetOpts);
Argyrios Kyrtzidisd7711ec2011-12-21 16:56:29 +000042 }
43
44 FileSystemOptions FileMgrOpts;
45 FileManager FileMgr;
Dylan Noblesmithc93dc782012-02-20 14:00:23 +000046 IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
Argyrios Kyrtzidisd7711ec2011-12-21 16:56:29 +000047 DiagnosticsEngine Diags;
48 SourceManager SourceMgr;
49 LangOptions LangOpts;
Douglas Gregor07f8cf42012-10-17 00:11:35 +000050 IntrusiveRefCntPtr<TargetOptions> TargetOpts;
Dylan Noblesmithc93dc782012-02-20 14:00:23 +000051 IntrusiveRefCntPtr<TargetInfo> Target;
Argyrios Kyrtzidisd7711ec2011-12-21 16:56:29 +000052};
53
54class VoidModuleLoader : public ModuleLoader {
Douglas Gregor7dff05b2012-11-30 00:01:57 +000055 virtual ModuleLoadResult loadModule(SourceLocation ImportLoc,
56 ModuleIdPath Path,
57 Module::NameVisibilityKind Visibility,
58 bool IsInclusionDirective) {
59 return ModuleLoadResult();
Argyrios Kyrtzidisd7711ec2011-12-21 16:56:29 +000060 }
NAKAMURA Takumi1a4191d2013-01-12 02:16:29 +000061
62 virtual void makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis5ebcb202013-02-01 16:36:12 +000063 Module::NameVisibilityKind Visibility,
64 SourceLocation ImportLoc) { }
Argyrios Kyrtzidisd7711ec2011-12-21 16:56:29 +000065};
66
67TEST_F(SourceManagerTest, isBeforeInTranslationUnit) {
68 const char *source =
69 "#define M(x) [x]\n"
70 "M(foo)";
71 MemoryBuffer *buf = MemoryBuffer::getMemBuffer(source);
72 FileID mainFileID = SourceMgr.createMainFileIDForMemBuffer(buf);
73
74 VoidModuleLoader ModLoader;
Douglas Gregorb0985c82012-10-24 16:24:38 +000075 HeaderSearch HeaderInfo(new HeaderSearchOptions, FileMgr, Diags, LangOpts,
76 &*Target);
Douglas Gregor36a16492012-10-24 17:46:57 +000077 Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, Target.getPtr(),
Argyrios Kyrtzidisd7711ec2011-12-21 16:56:29 +000078 SourceMgr, HeaderInfo, ModLoader,
79 /*IILookup =*/ 0,
80 /*OwnsHeaderSearch =*/false,
81 /*DelayInitialization =*/ false);
82 PP.EnterMainSourceFile();
83
84 std::vector<Token> toks;
85 while (1) {
86 Token tok;
87 PP.Lex(tok);
88 if (tok.is(tok::eof))
89 break;
90 toks.push_back(tok);
91 }
92
93 // Make sure we got the tokens that we expected.
94 ASSERT_EQ(3U, toks.size());
95 ASSERT_EQ(tok::l_square, toks[0].getKind());
96 ASSERT_EQ(tok::identifier, toks[1].getKind());
97 ASSERT_EQ(tok::r_square, toks[2].getKind());
98
99 SourceLocation lsqrLoc = toks[0].getLocation();
100 SourceLocation idLoc = toks[1].getLocation();
101 SourceLocation rsqrLoc = toks[2].getLocation();
102
103 SourceLocation macroExpStartLoc = SourceMgr.translateLineCol(mainFileID, 2, 1);
104 SourceLocation macroExpEndLoc = SourceMgr.translateLineCol(mainFileID, 2, 6);
105 ASSERT_TRUE(macroExpStartLoc.isFileID());
106 ASSERT_TRUE(macroExpEndLoc.isFileID());
107
Dylan Noblesmith36d59272012-02-13 12:32:26 +0000108 SmallString<32> str;
Argyrios Kyrtzidisd7711ec2011-12-21 16:56:29 +0000109 ASSERT_EQ("M", PP.getSpelling(macroExpStartLoc, str));
110 ASSERT_EQ(")", PP.getSpelling(macroExpEndLoc, str));
111
112 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(lsqrLoc, idLoc));
113 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, rsqrLoc));
114 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(macroExpStartLoc, idLoc));
115 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, macroExpEndLoc));
116}
117
Jordan Rose2e413f92012-06-19 03:09:38 +0000118TEST_F(SourceManagerTest, getColumnNumber) {
119 const char *Source =
120 "int x;\n"
121 "int y;";
122
123 MemoryBuffer *Buf = MemoryBuffer::getMemBuffer(Source);
124 FileID MainFileID = SourceMgr.createMainFileIDForMemBuffer(Buf);
125
126 bool Invalid;
127
128 Invalid = false;
129 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, &Invalid));
130 EXPECT_TRUE(!Invalid);
131
132 Invalid = false;
133 EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 4, &Invalid));
134 EXPECT_TRUE(!Invalid);
135
136 Invalid = false;
137 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 7, &Invalid));
138 EXPECT_TRUE(!Invalid);
139
140 Invalid = false;
141 EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 11, &Invalid));
142 EXPECT_TRUE(!Invalid);
143
144 Invalid = false;
145 EXPECT_EQ(7U, SourceMgr.getColumnNumber(MainFileID, strlen(Source),
146 &Invalid));
147 EXPECT_TRUE(!Invalid);
148
149 Invalid = false;
150 SourceMgr.getColumnNumber(MainFileID, strlen(Source)+1, &Invalid);
151 EXPECT_TRUE(Invalid);
152
153 // Test invalid files
154 Invalid = false;
155 SourceMgr.getColumnNumber(FileID(), 0, &Invalid);
156 EXPECT_TRUE(Invalid);
157
158 Invalid = false;
159 SourceMgr.getColumnNumber(FileID(), 1, &Invalid);
160 EXPECT_TRUE(Invalid);
161
162 // Test with no invalid flag.
163 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, NULL));
164}
165
Argyrios Kyrtzidiscee5ec92011-12-21 16:56:35 +0000166#if defined(LLVM_ON_UNIX)
167
168TEST_F(SourceManagerTest, getMacroArgExpandedLocation) {
169 const char *header =
170 "#define FM(x,y) x\n";
171
172 const char *main =
173 "#include \"/test-header.h\"\n"
174 "#define VAL 0\n"
175 "FM(VAL,0)\n"
176 "FM(0,VAL)\n"
177 "FM(FM(0,VAL),0)\n"
178 "#define CONCAT(X, Y) X##Y\n"
179 "CONCAT(1,1)\n";
180
181 MemoryBuffer *headerBuf = MemoryBuffer::getMemBuffer(header);
182 MemoryBuffer *mainBuf = MemoryBuffer::getMemBuffer(main);
183 FileID mainFileID = SourceMgr.createMainFileIDForMemBuffer(mainBuf);
184
185 const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
186 headerBuf->getBufferSize(), 0);
187 SourceMgr.overrideFileContents(headerFile, headerBuf);
188
189 VoidModuleLoader ModLoader;
Douglas Gregorb0985c82012-10-24 16:24:38 +0000190 HeaderSearch HeaderInfo(new HeaderSearchOptions, FileMgr, Diags, LangOpts,
191 &*Target);
Douglas Gregor36a16492012-10-24 17:46:57 +0000192 Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, Target.getPtr(),
Argyrios Kyrtzidiscee5ec92011-12-21 16:56:35 +0000193 SourceMgr, HeaderInfo, ModLoader,
194 /*IILookup =*/ 0,
195 /*OwnsHeaderSearch =*/false,
196 /*DelayInitialization =*/ false);
197 PP.EnterMainSourceFile();
198
199 std::vector<Token> toks;
200 while (1) {
201 Token tok;
202 PP.Lex(tok);
203 if (tok.is(tok::eof))
204 break;
205 toks.push_back(tok);
206 }
207
208 // Make sure we got the tokens that we expected.
209 ASSERT_EQ(4U, toks.size());
210 ASSERT_EQ(tok::numeric_constant, toks[0].getKind());
211 ASSERT_EQ(tok::numeric_constant, toks[1].getKind());
212 ASSERT_EQ(tok::numeric_constant, toks[2].getKind());
213 ASSERT_EQ(tok::numeric_constant, toks[3].getKind());
214
215 SourceLocation defLoc = SourceMgr.translateLineCol(mainFileID, 2, 13);
216 SourceLocation loc1 = SourceMgr.translateLineCol(mainFileID, 3, 8);
217 SourceLocation loc2 = SourceMgr.translateLineCol(mainFileID, 4, 4);
218 SourceLocation loc3 = SourceMgr.translateLineCol(mainFileID, 5, 7);
219 SourceLocation defLoc2 = SourceMgr.translateLineCol(mainFileID, 6, 22);
220 defLoc = SourceMgr.getMacroArgExpandedLocation(defLoc);
221 loc1 = SourceMgr.getMacroArgExpandedLocation(loc1);
222 loc2 = SourceMgr.getMacroArgExpandedLocation(loc2);
223 loc3 = SourceMgr.getMacroArgExpandedLocation(loc3);
224 defLoc2 = SourceMgr.getMacroArgExpandedLocation(defLoc2);
225
226 EXPECT_TRUE(defLoc.isFileID());
227 EXPECT_TRUE(loc1.isFileID());
228 EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc2));
229 EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc3));
230 EXPECT_EQ(loc2, toks[1].getLocation());
231 EXPECT_EQ(loc3, toks[2].getLocation());
232 EXPECT_TRUE(defLoc2.isFileID());
233}
234
Argyrios Kyrtzidisdb81d382012-03-27 18:47:48 +0000235namespace {
236
237struct MacroAction {
238 SourceLocation Loc;
239 std::string Name;
240 bool isDefinition; // if false, it is expansion.
241
242 MacroAction(SourceLocation Loc, StringRef Name, bool isDefinition)
243 : Loc(Loc), Name(Name), isDefinition(isDefinition) { }
244};
245
246class MacroTracker : public PPCallbacks {
247 std::vector<MacroAction> &Macros;
248
249public:
250 explicit MacroTracker(std::vector<MacroAction> &Macros) : Macros(Macros) { }
251
Argyrios Kyrtzidisc5159782013-02-24 00:05:14 +0000252 virtual void MacroDefined(const Token &MacroNameTok,
253 const MacroDirective *MD) {
254 Macros.push_back(MacroAction(MD->getLocation(),
Argyrios Kyrtzidisdb81d382012-03-27 18:47:48 +0000255 MacroNameTok.getIdentifierInfo()->getName(),
256 true));
257 }
Argyrios Kyrtzidisc5159782013-02-24 00:05:14 +0000258 virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
Argyrios Kyrtzidisdb81d382012-03-27 18:47:48 +0000259 SourceRange Range) {
260 Macros.push_back(MacroAction(MacroNameTok.getLocation(),
261 MacroNameTok.getIdentifierInfo()->getName(),
262 false));
263 }
264};
265
266}
267
268TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) {
269 const char *header =
270 "#define MACRO_IN_INCLUDE 0\n";
271
272 const char *main =
273 "#define M(x) x\n"
274 "#define INC \"/test-header.h\"\n"
275 "#include M(INC)\n"
276 "#define INC2 </test-header.h>\n"
277 "#include M(INC2)\n";
278
279 MemoryBuffer *headerBuf = MemoryBuffer::getMemBuffer(header);
280 MemoryBuffer *mainBuf = MemoryBuffer::getMemBuffer(main);
281 SourceMgr.createMainFileIDForMemBuffer(mainBuf);
282
283 const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
284 headerBuf->getBufferSize(), 0);
285 SourceMgr.overrideFileContents(headerFile, headerBuf);
286
287 VoidModuleLoader ModLoader;
Douglas Gregorb0985c82012-10-24 16:24:38 +0000288 HeaderSearch HeaderInfo(new HeaderSearchOptions, FileMgr, Diags, LangOpts,
289 &*Target);
Douglas Gregor36a16492012-10-24 17:46:57 +0000290 Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, Target.getPtr(),
Argyrios Kyrtzidisdb81d382012-03-27 18:47:48 +0000291 SourceMgr, HeaderInfo, ModLoader,
292 /*IILookup =*/ 0,
293 /*OwnsHeaderSearch =*/false,
294 /*DelayInitialization =*/ false);
295
296 std::vector<MacroAction> Macros;
297 PP.addPPCallbacks(new MacroTracker(Macros));
298
299 PP.EnterMainSourceFile();
300
301 std::vector<Token> toks;
302 while (1) {
303 Token tok;
304 PP.Lex(tok);
305 if (tok.is(tok::eof))
306 break;
307 toks.push_back(tok);
308 }
309
310 // Make sure we got the tokens that we expected.
311 ASSERT_EQ(0U, toks.size());
312
313 ASSERT_EQ(9U, Macros.size());
314 // #define M(x) x
315 ASSERT_TRUE(Macros[0].isDefinition);
316 ASSERT_EQ("M", Macros[0].Name);
317 // #define INC "/test-header.h"
318 ASSERT_TRUE(Macros[1].isDefinition);
319 ASSERT_EQ("INC", Macros[1].Name);
320 // M expansion in #include M(INC)
321 ASSERT_FALSE(Macros[2].isDefinition);
322 ASSERT_EQ("M", Macros[2].Name);
323 // INC expansion in #include M(INC)
324 ASSERT_FALSE(Macros[3].isDefinition);
325 ASSERT_EQ("INC", Macros[3].Name);
326 // #define MACRO_IN_INCLUDE 0
327 ASSERT_TRUE(Macros[4].isDefinition);
328 ASSERT_EQ("MACRO_IN_INCLUDE", Macros[4].Name);
329 // #define INC2 </test-header.h>
330 ASSERT_TRUE(Macros[5].isDefinition);
331 ASSERT_EQ("INC2", Macros[5].Name);
332 // M expansion in #include M(INC2)
333 ASSERT_FALSE(Macros[6].isDefinition);
334 ASSERT_EQ("M", Macros[6].Name);
335 // INC2 expansion in #include M(INC2)
336 ASSERT_FALSE(Macros[7].isDefinition);
337 ASSERT_EQ("INC2", Macros[7].Name);
338 // #define MACRO_IN_INCLUDE 0
339 ASSERT_TRUE(Macros[8].isDefinition);
340 ASSERT_EQ("MACRO_IN_INCLUDE", Macros[8].Name);
341
342 // The INC expansion in #include M(INC) comes before the first
343 // MACRO_IN_INCLUDE definition of the included file.
344 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[3].Loc, Macros[4].Loc));
345
346 // The INC2 expansion in #include M(INC2) comes before the second
347 // MACRO_IN_INCLUDE definition of the included file.
348 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[7].Loc, Macros[8].Loc));
349}
350
Argyrios Kyrtzidiscee5ec92011-12-21 16:56:35 +0000351#endif
352
Argyrios Kyrtzidisd7711ec2011-12-21 16:56:29 +0000353} // anonymous namespace