blob: 443d07714527f9534eaf099c33fa2babe668ea02 [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,
63 Module::NameVisibilityKind Visibility) { }
Argyrios Kyrtzidisd7711ec2011-12-21 16:56:29 +000064};
65
66TEST_F(SourceManagerTest, isBeforeInTranslationUnit) {
67 const char *source =
68 "#define M(x) [x]\n"
69 "M(foo)";
70 MemoryBuffer *buf = MemoryBuffer::getMemBuffer(source);
71 FileID mainFileID = SourceMgr.createMainFileIDForMemBuffer(buf);
72
73 VoidModuleLoader ModLoader;
Douglas Gregorb0985c82012-10-24 16:24:38 +000074 HeaderSearch HeaderInfo(new HeaderSearchOptions, FileMgr, Diags, LangOpts,
75 &*Target);
Douglas Gregor36a16492012-10-24 17:46:57 +000076 Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, Target.getPtr(),
Argyrios Kyrtzidisd7711ec2011-12-21 16:56:29 +000077 SourceMgr, HeaderInfo, ModLoader,
78 /*IILookup =*/ 0,
79 /*OwnsHeaderSearch =*/false,
80 /*DelayInitialization =*/ false);
81 PP.EnterMainSourceFile();
82
83 std::vector<Token> toks;
84 while (1) {
85 Token tok;
86 PP.Lex(tok);
87 if (tok.is(tok::eof))
88 break;
89 toks.push_back(tok);
90 }
91
92 // Make sure we got the tokens that we expected.
93 ASSERT_EQ(3U, toks.size());
94 ASSERT_EQ(tok::l_square, toks[0].getKind());
95 ASSERT_EQ(tok::identifier, toks[1].getKind());
96 ASSERT_EQ(tok::r_square, toks[2].getKind());
97
98 SourceLocation lsqrLoc = toks[0].getLocation();
99 SourceLocation idLoc = toks[1].getLocation();
100 SourceLocation rsqrLoc = toks[2].getLocation();
101
102 SourceLocation macroExpStartLoc = SourceMgr.translateLineCol(mainFileID, 2, 1);
103 SourceLocation macroExpEndLoc = SourceMgr.translateLineCol(mainFileID, 2, 6);
104 ASSERT_TRUE(macroExpStartLoc.isFileID());
105 ASSERT_TRUE(macroExpEndLoc.isFileID());
106
Dylan Noblesmith36d59272012-02-13 12:32:26 +0000107 SmallString<32> str;
Argyrios Kyrtzidisd7711ec2011-12-21 16:56:29 +0000108 ASSERT_EQ("M", PP.getSpelling(macroExpStartLoc, str));
109 ASSERT_EQ(")", PP.getSpelling(macroExpEndLoc, str));
110
111 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(lsqrLoc, idLoc));
112 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, rsqrLoc));
113 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(macroExpStartLoc, idLoc));
114 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, macroExpEndLoc));
115}
116
Jordan Rose2e413f92012-06-19 03:09:38 +0000117TEST_F(SourceManagerTest, getColumnNumber) {
118 const char *Source =
119 "int x;\n"
120 "int y;";
121
122 MemoryBuffer *Buf = MemoryBuffer::getMemBuffer(Source);
123 FileID MainFileID = SourceMgr.createMainFileIDForMemBuffer(Buf);
124
125 bool Invalid;
126
127 Invalid = false;
128 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, &Invalid));
129 EXPECT_TRUE(!Invalid);
130
131 Invalid = false;
132 EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 4, &Invalid));
133 EXPECT_TRUE(!Invalid);
134
135 Invalid = false;
136 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 7, &Invalid));
137 EXPECT_TRUE(!Invalid);
138
139 Invalid = false;
140 EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 11, &Invalid));
141 EXPECT_TRUE(!Invalid);
142
143 Invalid = false;
144 EXPECT_EQ(7U, SourceMgr.getColumnNumber(MainFileID, strlen(Source),
145 &Invalid));
146 EXPECT_TRUE(!Invalid);
147
148 Invalid = false;
149 SourceMgr.getColumnNumber(MainFileID, strlen(Source)+1, &Invalid);
150 EXPECT_TRUE(Invalid);
151
152 // Test invalid files
153 Invalid = false;
154 SourceMgr.getColumnNumber(FileID(), 0, &Invalid);
155 EXPECT_TRUE(Invalid);
156
157 Invalid = false;
158 SourceMgr.getColumnNumber(FileID(), 1, &Invalid);
159 EXPECT_TRUE(Invalid);
160
161 // Test with no invalid flag.
162 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, NULL));
163}
164
Argyrios Kyrtzidiscee5ec92011-12-21 16:56:35 +0000165#if defined(LLVM_ON_UNIX)
166
167TEST_F(SourceManagerTest, getMacroArgExpandedLocation) {
168 const char *header =
169 "#define FM(x,y) x\n";
170
171 const char *main =
172 "#include \"/test-header.h\"\n"
173 "#define VAL 0\n"
174 "FM(VAL,0)\n"
175 "FM(0,VAL)\n"
176 "FM(FM(0,VAL),0)\n"
177 "#define CONCAT(X, Y) X##Y\n"
178 "CONCAT(1,1)\n";
179
180 MemoryBuffer *headerBuf = MemoryBuffer::getMemBuffer(header);
181 MemoryBuffer *mainBuf = MemoryBuffer::getMemBuffer(main);
182 FileID mainFileID = SourceMgr.createMainFileIDForMemBuffer(mainBuf);
183
184 const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
185 headerBuf->getBufferSize(), 0);
186 SourceMgr.overrideFileContents(headerFile, headerBuf);
187
188 VoidModuleLoader ModLoader;
Douglas Gregorb0985c82012-10-24 16:24:38 +0000189 HeaderSearch HeaderInfo(new HeaderSearchOptions, FileMgr, Diags, LangOpts,
190 &*Target);
Douglas Gregor36a16492012-10-24 17:46:57 +0000191 Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, Target.getPtr(),
Argyrios Kyrtzidiscee5ec92011-12-21 16:56:35 +0000192 SourceMgr, HeaderInfo, ModLoader,
193 /*IILookup =*/ 0,
194 /*OwnsHeaderSearch =*/false,
195 /*DelayInitialization =*/ false);
196 PP.EnterMainSourceFile();
197
198 std::vector<Token> toks;
199 while (1) {
200 Token tok;
201 PP.Lex(tok);
202 if (tok.is(tok::eof))
203 break;
204 toks.push_back(tok);
205 }
206
207 // Make sure we got the tokens that we expected.
208 ASSERT_EQ(4U, toks.size());
209 ASSERT_EQ(tok::numeric_constant, toks[0].getKind());
210 ASSERT_EQ(tok::numeric_constant, toks[1].getKind());
211 ASSERT_EQ(tok::numeric_constant, toks[2].getKind());
212 ASSERT_EQ(tok::numeric_constant, toks[3].getKind());
213
214 SourceLocation defLoc = SourceMgr.translateLineCol(mainFileID, 2, 13);
215 SourceLocation loc1 = SourceMgr.translateLineCol(mainFileID, 3, 8);
216 SourceLocation loc2 = SourceMgr.translateLineCol(mainFileID, 4, 4);
217 SourceLocation loc3 = SourceMgr.translateLineCol(mainFileID, 5, 7);
218 SourceLocation defLoc2 = SourceMgr.translateLineCol(mainFileID, 6, 22);
219 defLoc = SourceMgr.getMacroArgExpandedLocation(defLoc);
220 loc1 = SourceMgr.getMacroArgExpandedLocation(loc1);
221 loc2 = SourceMgr.getMacroArgExpandedLocation(loc2);
222 loc3 = SourceMgr.getMacroArgExpandedLocation(loc3);
223 defLoc2 = SourceMgr.getMacroArgExpandedLocation(defLoc2);
224
225 EXPECT_TRUE(defLoc.isFileID());
226 EXPECT_TRUE(loc1.isFileID());
227 EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc2));
228 EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc3));
229 EXPECT_EQ(loc2, toks[1].getLocation());
230 EXPECT_EQ(loc3, toks[2].getLocation());
231 EXPECT_TRUE(defLoc2.isFileID());
232}
233
Argyrios Kyrtzidisdb81d382012-03-27 18:47:48 +0000234namespace {
235
236struct MacroAction {
237 SourceLocation Loc;
238 std::string Name;
239 bool isDefinition; // if false, it is expansion.
240
241 MacroAction(SourceLocation Loc, StringRef Name, bool isDefinition)
242 : Loc(Loc), Name(Name), isDefinition(isDefinition) { }
243};
244
245class MacroTracker : public PPCallbacks {
246 std::vector<MacroAction> &Macros;
247
248public:
249 explicit MacroTracker(std::vector<MacroAction> &Macros) : Macros(Macros) { }
250
251 virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) {
252 Macros.push_back(MacroAction(MI->getDefinitionLoc(),
253 MacroNameTok.getIdentifierInfo()->getName(),
254 true));
255 }
256 virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo* MI,
257 SourceRange Range) {
258 Macros.push_back(MacroAction(MacroNameTok.getLocation(),
259 MacroNameTok.getIdentifierInfo()->getName(),
260 false));
261 }
262};
263
264}
265
266TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) {
267 const char *header =
268 "#define MACRO_IN_INCLUDE 0\n";
269
270 const char *main =
271 "#define M(x) x\n"
272 "#define INC \"/test-header.h\"\n"
273 "#include M(INC)\n"
274 "#define INC2 </test-header.h>\n"
275 "#include M(INC2)\n";
276
277 MemoryBuffer *headerBuf = MemoryBuffer::getMemBuffer(header);
278 MemoryBuffer *mainBuf = MemoryBuffer::getMemBuffer(main);
279 SourceMgr.createMainFileIDForMemBuffer(mainBuf);
280
281 const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
282 headerBuf->getBufferSize(), 0);
283 SourceMgr.overrideFileContents(headerFile, headerBuf);
284
285 VoidModuleLoader ModLoader;
Douglas Gregorb0985c82012-10-24 16:24:38 +0000286 HeaderSearch HeaderInfo(new HeaderSearchOptions, FileMgr, Diags, LangOpts,
287 &*Target);
Douglas Gregor36a16492012-10-24 17:46:57 +0000288 Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, Target.getPtr(),
Argyrios Kyrtzidisdb81d382012-03-27 18:47:48 +0000289 SourceMgr, HeaderInfo, ModLoader,
290 /*IILookup =*/ 0,
291 /*OwnsHeaderSearch =*/false,
292 /*DelayInitialization =*/ false);
293
294 std::vector<MacroAction> Macros;
295 PP.addPPCallbacks(new MacroTracker(Macros));
296
297 PP.EnterMainSourceFile();
298
299 std::vector<Token> toks;
300 while (1) {
301 Token tok;
302 PP.Lex(tok);
303 if (tok.is(tok::eof))
304 break;
305 toks.push_back(tok);
306 }
307
308 // Make sure we got the tokens that we expected.
309 ASSERT_EQ(0U, toks.size());
310
311 ASSERT_EQ(9U, Macros.size());
312 // #define M(x) x
313 ASSERT_TRUE(Macros[0].isDefinition);
314 ASSERT_EQ("M", Macros[0].Name);
315 // #define INC "/test-header.h"
316 ASSERT_TRUE(Macros[1].isDefinition);
317 ASSERT_EQ("INC", Macros[1].Name);
318 // M expansion in #include M(INC)
319 ASSERT_FALSE(Macros[2].isDefinition);
320 ASSERT_EQ("M", Macros[2].Name);
321 // INC expansion in #include M(INC)
322 ASSERT_FALSE(Macros[3].isDefinition);
323 ASSERT_EQ("INC", Macros[3].Name);
324 // #define MACRO_IN_INCLUDE 0
325 ASSERT_TRUE(Macros[4].isDefinition);
326 ASSERT_EQ("MACRO_IN_INCLUDE", Macros[4].Name);
327 // #define INC2 </test-header.h>
328 ASSERT_TRUE(Macros[5].isDefinition);
329 ASSERT_EQ("INC2", Macros[5].Name);
330 // M expansion in #include M(INC2)
331 ASSERT_FALSE(Macros[6].isDefinition);
332 ASSERT_EQ("M", Macros[6].Name);
333 // INC2 expansion in #include M(INC2)
334 ASSERT_FALSE(Macros[7].isDefinition);
335 ASSERT_EQ("INC2", Macros[7].Name);
336 // #define MACRO_IN_INCLUDE 0
337 ASSERT_TRUE(Macros[8].isDefinition);
338 ASSERT_EQ("MACRO_IN_INCLUDE", Macros[8].Name);
339
340 // The INC expansion in #include M(INC) comes before the first
341 // MACRO_IN_INCLUDE definition of the included file.
342 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[3].Loc, Macros[4].Loc));
343
344 // The INC2 expansion in #include M(INC2) comes before the second
345 // MACRO_IN_INCLUDE definition of the included file.
346 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[7].Loc, Macros[8].Loc));
347}
348
Argyrios Kyrtzidiscee5ec92011-12-21 16:56:35 +0000349#endif
350
Argyrios Kyrtzidisd7711ec2011-12-21 16:56:29 +0000351} // anonymous namespace