1 //===- unittests/Basic/SourceManagerTest.cpp ------ SourceManager tests ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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/MemoryBufferCache.h"
16 #include "clang/Basic/TargetInfo.h"
17 #include "clang/Basic/TargetOptions.h"
18 #include "clang/Lex/HeaderSearch.h"
19 #include "clang/Lex/HeaderSearchOptions.h"
20 #include "clang/Lex/ModuleLoader.h"
21 #include "clang/Lex/Preprocessor.h"
22 #include "clang/Lex/PreprocessorOptions.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/Config/llvm-config.h"
25 #include "gtest/gtest.h"
27 using namespace clang;
32 class SourceManagerTest : public ::testing::Test {
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);
44 FileSystemOptions FileMgrOpts;
46 IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
47 DiagnosticsEngine Diags;
48 SourceManager SourceMgr;
50 std::shared_ptr<TargetOptions> TargetOpts;
51 IntrusiveRefCntPtr<TargetInfo> Target;
54 class VoidModuleLoader : public ModuleLoader {
55 ModuleLoadResult loadModule(SourceLocation ImportLoc,
57 Module::NameVisibilityKind Visibility,
58 bool IsInclusionDirective) override {
59 return ModuleLoadResult();
62 void makeModuleVisible(Module *Mod,
63 Module::NameVisibilityKind Visibility,
64 SourceLocation ImportLoc) override { }
66 GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
68 bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
72 TEST_F(SourceManagerTest, isBeforeInTranslationUnit) {
76 std::unique_ptr<llvm::MemoryBuffer> Buf =
77 llvm::MemoryBuffer::getMemBuffer(source);
78 FileID mainFileID = SourceMgr.createFileID(std::move(Buf));
79 SourceMgr.setMainFileID(mainFileID);
81 VoidModuleLoader ModLoader;
82 MemoryBufferCache PCMCache;
83 HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
84 Diags, LangOpts, &*Target);
85 Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
86 SourceMgr, PCMCache, HeaderInfo, ModLoader,
87 /*IILookup =*/nullptr,
88 /*OwnsHeaderSearch =*/false);
89 PP.Initialize(*Target);
90 PP.EnterMainSourceFile();
92 std::vector<Token> toks;
101 // Make sure we got the tokens that we expected.
102 ASSERT_EQ(3U, toks.size());
103 ASSERT_EQ(tok::l_square, toks[0].getKind());
104 ASSERT_EQ(tok::identifier, toks[1].getKind());
105 ASSERT_EQ(tok::r_square, toks[2].getKind());
107 SourceLocation lsqrLoc = toks[0].getLocation();
108 SourceLocation idLoc = toks[1].getLocation();
109 SourceLocation rsqrLoc = toks[2].getLocation();
111 SourceLocation macroExpStartLoc = SourceMgr.translateLineCol(mainFileID, 2, 1);
112 SourceLocation macroExpEndLoc = SourceMgr.translateLineCol(mainFileID, 2, 6);
113 ASSERT_TRUE(macroExpStartLoc.isFileID());
114 ASSERT_TRUE(macroExpEndLoc.isFileID());
117 ASSERT_EQ("M", PP.getSpelling(macroExpStartLoc, str));
118 ASSERT_EQ(")", PP.getSpelling(macroExpEndLoc, str));
120 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(lsqrLoc, idLoc));
121 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, rsqrLoc));
122 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(macroExpStartLoc, idLoc));
123 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, macroExpEndLoc));
126 TEST_F(SourceManagerTest, getColumnNumber) {
131 std::unique_ptr<llvm::MemoryBuffer> Buf =
132 llvm::MemoryBuffer::getMemBuffer(Source);
133 FileID MainFileID = SourceMgr.createFileID(std::move(Buf));
134 SourceMgr.setMainFileID(MainFileID);
139 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, &Invalid));
140 EXPECT_TRUE(!Invalid);
143 EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 4, &Invalid));
144 EXPECT_TRUE(!Invalid);
147 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 7, &Invalid));
148 EXPECT_TRUE(!Invalid);
151 EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 11, &Invalid));
152 EXPECT_TRUE(!Invalid);
155 EXPECT_EQ(7U, SourceMgr.getColumnNumber(MainFileID, strlen(Source),
157 EXPECT_TRUE(!Invalid);
160 SourceMgr.getColumnNumber(MainFileID, strlen(Source)+1, &Invalid);
161 EXPECT_TRUE(Invalid);
163 // Test invalid files
165 SourceMgr.getColumnNumber(FileID(), 0, &Invalid);
166 EXPECT_TRUE(Invalid);
169 SourceMgr.getColumnNumber(FileID(), 1, &Invalid);
170 EXPECT_TRUE(Invalid);
172 // Test with no invalid flag.
173 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, nullptr));
176 #if defined(LLVM_ON_UNIX)
178 TEST_F(SourceManagerTest, getMacroArgExpandedLocation) {
180 "#define FM(x,y) x\n";
183 "#include \"/test-header.h\"\n"
188 "#define CONCAT(X, Y) X##Y\n"
191 std::unique_ptr<llvm::MemoryBuffer> HeaderBuf =
192 llvm::MemoryBuffer::getMemBuffer(header);
193 std::unique_ptr<llvm::MemoryBuffer> MainBuf =
194 llvm::MemoryBuffer::getMemBuffer(main);
195 FileID mainFileID = SourceMgr.createFileID(std::move(MainBuf));
196 SourceMgr.setMainFileID(mainFileID);
198 const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
199 HeaderBuf->getBufferSize(), 0);
200 SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
202 VoidModuleLoader ModLoader;
203 MemoryBufferCache PCMCache;
204 HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
205 Diags, LangOpts, &*Target);
206 Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
207 SourceMgr, PCMCache, HeaderInfo, ModLoader,
208 /*IILookup =*/nullptr,
209 /*OwnsHeaderSearch =*/false);
210 PP.Initialize(*Target);
211 PP.EnterMainSourceFile();
213 std::vector<Token> toks;
217 if (tok.is(tok::eof))
222 // Make sure we got the tokens that we expected.
223 ASSERT_EQ(4U, toks.size());
224 ASSERT_EQ(tok::numeric_constant, toks[0].getKind());
225 ASSERT_EQ(tok::numeric_constant, toks[1].getKind());
226 ASSERT_EQ(tok::numeric_constant, toks[2].getKind());
227 ASSERT_EQ(tok::numeric_constant, toks[3].getKind());
229 SourceLocation defLoc = SourceMgr.translateLineCol(mainFileID, 2, 13);
230 SourceLocation loc1 = SourceMgr.translateLineCol(mainFileID, 3, 8);
231 SourceLocation loc2 = SourceMgr.translateLineCol(mainFileID, 4, 4);
232 SourceLocation loc3 = SourceMgr.translateLineCol(mainFileID, 5, 7);
233 SourceLocation defLoc2 = SourceMgr.translateLineCol(mainFileID, 6, 22);
234 defLoc = SourceMgr.getMacroArgExpandedLocation(defLoc);
235 loc1 = SourceMgr.getMacroArgExpandedLocation(loc1);
236 loc2 = SourceMgr.getMacroArgExpandedLocation(loc2);
237 loc3 = SourceMgr.getMacroArgExpandedLocation(loc3);
238 defLoc2 = SourceMgr.getMacroArgExpandedLocation(defLoc2);
240 EXPECT_TRUE(defLoc.isFileID());
241 EXPECT_TRUE(loc1.isFileID());
242 EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc2));
243 EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc3));
244 EXPECT_EQ(loc2, toks[1].getLocation());
245 EXPECT_EQ(loc3, toks[2].getLocation());
246 EXPECT_TRUE(defLoc2.isFileID());
254 bool isDefinition; // if false, it is expansion.
256 MacroAction(SourceLocation Loc, StringRef Name, bool isDefinition)
257 : Loc(Loc), Name(Name), isDefinition(isDefinition) { }
260 class MacroTracker : public PPCallbacks {
261 std::vector<MacroAction> &Macros;
264 explicit MacroTracker(std::vector<MacroAction> &Macros) : Macros(Macros) { }
266 void MacroDefined(const Token &MacroNameTok,
267 const MacroDirective *MD) override {
268 Macros.push_back(MacroAction(MD->getLocation(),
269 MacroNameTok.getIdentifierInfo()->getName(),
272 void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
273 SourceRange Range, const MacroArgs *Args) override {
274 Macros.push_back(MacroAction(MacroNameTok.getLocation(),
275 MacroNameTok.getIdentifierInfo()->getName(),
282 TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) {
284 "#define MACRO_IN_INCLUDE 0\n";
288 "#define INC \"/test-header.h\"\n"
290 "#define INC2 </test-header.h>\n"
291 "#include M(INC2)\n";
293 std::unique_ptr<llvm::MemoryBuffer> HeaderBuf =
294 llvm::MemoryBuffer::getMemBuffer(header);
295 std::unique_ptr<llvm::MemoryBuffer> MainBuf =
296 llvm::MemoryBuffer::getMemBuffer(main);
297 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(MainBuf)));
299 const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
300 HeaderBuf->getBufferSize(), 0);
301 SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
303 VoidModuleLoader ModLoader;
304 MemoryBufferCache PCMCache;
305 HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
306 Diags, LangOpts, &*Target);
307 Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
308 SourceMgr, PCMCache, HeaderInfo, ModLoader,
309 /*IILookup =*/nullptr,
310 /*OwnsHeaderSearch =*/false);
311 PP.Initialize(*Target);
313 std::vector<MacroAction> Macros;
314 PP.addPPCallbacks(llvm::make_unique<MacroTracker>(Macros));
316 PP.EnterMainSourceFile();
318 std::vector<Token> toks;
322 if (tok.is(tok::eof))
327 // Make sure we got the tokens that we expected.
328 ASSERT_EQ(0U, toks.size());
330 ASSERT_EQ(9U, Macros.size());
332 ASSERT_TRUE(Macros[0].isDefinition);
333 ASSERT_EQ("M", Macros[0].Name);
334 // #define INC "/test-header.h"
335 ASSERT_TRUE(Macros[1].isDefinition);
336 ASSERT_EQ("INC", Macros[1].Name);
337 // M expansion in #include M(INC)
338 ASSERT_FALSE(Macros[2].isDefinition);
339 ASSERT_EQ("M", Macros[2].Name);
340 // INC expansion in #include M(INC)
341 ASSERT_FALSE(Macros[3].isDefinition);
342 ASSERT_EQ("INC", Macros[3].Name);
343 // #define MACRO_IN_INCLUDE 0
344 ASSERT_TRUE(Macros[4].isDefinition);
345 ASSERT_EQ("MACRO_IN_INCLUDE", Macros[4].Name);
346 // #define INC2 </test-header.h>
347 ASSERT_TRUE(Macros[5].isDefinition);
348 ASSERT_EQ("INC2", Macros[5].Name);
349 // M expansion in #include M(INC2)
350 ASSERT_FALSE(Macros[6].isDefinition);
351 ASSERT_EQ("M", Macros[6].Name);
352 // INC2 expansion in #include M(INC2)
353 ASSERT_FALSE(Macros[7].isDefinition);
354 ASSERT_EQ("INC2", Macros[7].Name);
355 // #define MACRO_IN_INCLUDE 0
356 ASSERT_TRUE(Macros[8].isDefinition);
357 ASSERT_EQ("MACRO_IN_INCLUDE", Macros[8].Name);
359 // The INC expansion in #include M(INC) comes before the first
360 // MACRO_IN_INCLUDE definition of the included file.
361 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[3].Loc, Macros[4].Loc));
363 // The INC2 expansion in #include M(INC2) comes before the second
364 // MACRO_IN_INCLUDE definition of the included file.
365 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[7].Loc, Macros[8].Loc));
370 } // anonymous namespace