1 //===- 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/Diagnostic.h"
12 #include "clang/Basic/DiagnosticOptions.h"
13 #include "clang/Basic/FileManager.h"
14 #include "clang/Basic/LangOptions.h"
15 #include "clang/Basic/TargetInfo.h"
16 #include "clang/Basic/TargetOptions.h"
17 #include "clang/Lex/HeaderSearch.h"
18 #include "clang/Lex/HeaderSearchOptions.h"
19 #include "clang/Lex/ModuleLoader.h"
20 #include "clang/Lex/Preprocessor.h"
21 #include "clang/Lex/PreprocessorOptions.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/Config/config.h"
24 #include "gtest/gtest.h"
25
26 using namespace llvm;
27 using namespace clang;
28
29 namespace {
30
31 // The test fixture.
32 class SourceManagerTest : public ::testing::Test {
33 protected:
SourceManagerTest()34 SourceManagerTest()
35 : FileMgr(FileMgrOpts),
36 DiagID(new DiagnosticIDs()),
37 Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
38 SourceMgr(Diags, FileMgr),
39 TargetOpts(new TargetOptions) {
40 TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
41 Target = TargetInfo::CreateTargetInfo(Diags, &*TargetOpts);
42 }
43
44 FileSystemOptions FileMgrOpts;
45 FileManager FileMgr;
46 IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
47 DiagnosticsEngine Diags;
48 SourceManager SourceMgr;
49 LangOptions LangOpts;
50 IntrusiveRefCntPtr<TargetOptions> TargetOpts;
51 IntrusiveRefCntPtr<TargetInfo> Target;
52 };
53
54 class VoidModuleLoader : public ModuleLoader {
loadModule(SourceLocation ImportLoc,ModuleIdPath Path,Module::NameVisibilityKind Visibility,bool IsInclusionDirective)55 virtual ModuleLoadResult loadModule(SourceLocation ImportLoc,
56 ModuleIdPath Path,
57 Module::NameVisibilityKind Visibility,
58 bool IsInclusionDirective) {
59 return ModuleLoadResult();
60 }
61
makeModuleVisible(Module * Mod,Module::NameVisibilityKind Visibility,SourceLocation ImportLoc)62 virtual void makeModuleVisible(Module *Mod,
63 Module::NameVisibilityKind Visibility,
64 SourceLocation ImportLoc) { }
65 };
66
TEST_F(SourceManagerTest,isBeforeInTranslationUnit)67 TEST_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;
75 HeaderSearch HeaderInfo(new HeaderSearchOptions, FileMgr, Diags, LangOpts,
76 &*Target);
77 Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, Target.getPtr(),
78 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
108 SmallString<32> str;
109 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
TEST_F(SourceManagerTest,getColumnNumber)118 TEST_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
166 #if defined(LLVM_ON_UNIX)
167
TEST_F(SourceManagerTest,getMacroArgExpandedLocation)168 TEST_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;
190 HeaderSearch HeaderInfo(new HeaderSearchOptions, FileMgr, Diags, LangOpts,
191 &*Target);
192 Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, Target.getPtr(),
193 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
235 namespace {
236
237 struct MacroAction {
238 SourceLocation Loc;
239 std::string Name;
240 bool isDefinition; // if false, it is expansion.
241
MacroAction__anon38092bfb0111::__anon38092bfb0211::MacroAction242 MacroAction(SourceLocation Loc, StringRef Name, bool isDefinition)
243 : Loc(Loc), Name(Name), isDefinition(isDefinition) { }
244 };
245
246 class MacroTracker : public PPCallbacks {
247 std::vector<MacroAction> &Macros;
248
249 public:
MacroTracker(std::vector<MacroAction> & Macros)250 explicit MacroTracker(std::vector<MacroAction> &Macros) : Macros(Macros) { }
251
MacroDefined(const Token & MacroNameTok,const MacroDirective * MD)252 virtual void MacroDefined(const Token &MacroNameTok,
253 const MacroDirective *MD) {
254 Macros.push_back(MacroAction(MD->getLocation(),
255 MacroNameTok.getIdentifierInfo()->getName(),
256 true));
257 }
MacroExpands(const Token & MacroNameTok,const MacroDirective * MD,SourceRange Range)258 virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
259 SourceRange Range) {
260 Macros.push_back(MacroAction(MacroNameTok.getLocation(),
261 MacroNameTok.getIdentifierInfo()->getName(),
262 false));
263 }
264 };
265
266 }
267
TEST_F(SourceManagerTest,isBeforeInTranslationUnitWithMacroInInclude)268 TEST_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;
288 HeaderSearch HeaderInfo(new HeaderSearchOptions, FileMgr, Diags, LangOpts,
289 &*Target);
290 Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, Target.getPtr(),
291 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
351 #endif
352
353 } // anonymous namespace
354