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