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