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