xref: /llvm-project/clang/unittests/Basic/SourceManagerTest.cpp (revision e73d2a9fff82a5c22d923d47cdf81593e4b9425b)
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:
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 {
55   virtual ModuleLoadResult loadModule(SourceLocation ImportLoc,
56                                       ModuleIdPath Path,
57                                       Module::NameVisibilityKind Visibility,
58                                       bool IsInclusionDirective) {
59     return ModuleLoadResult();
60   }
61 
62   virtual void makeModuleVisible(Module *Mod,
63                                  Module::NameVisibilityKind Visibility) { }
64 };
65 
66 TEST_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;
74   HeaderSearch HeaderInfo(new HeaderSearchOptions, FileMgr, Diags, LangOpts,
75                           &*Target);
76   Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, Target.getPtr(),
77                   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 
107   SmallString<32> str;
108   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 
117 TEST_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 
165 #if defined(LLVM_ON_UNIX)
166 
167 TEST_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;
189   HeaderSearch HeaderInfo(new HeaderSearchOptions, FileMgr, Diags, LangOpts,
190                           &*Target);
191   Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, Target.getPtr(),
192                   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 
234 namespace {
235 
236 struct 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 
245 class MacroTracker : public PPCallbacks {
246   std::vector<MacroAction> &Macros;
247 
248 public:
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 
266 TEST_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;
286   HeaderSearch HeaderInfo(new HeaderSearchOptions, FileMgr, Diags, LangOpts,
287                           &*Target);
288   Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, Target.getPtr(),
289                   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 
349 #endif
350 
351 } // anonymous namespace
352