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