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