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