blob: a967b0ec7c21c612037cf4c9e986bdfebfaa6c49 [file] [log] [blame]
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +00001//===- 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"
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000011#include "clang/Basic/Diagnostic.h"
Douglas Gregoredf8e382012-10-23 22:38:58 +000012#include "clang/Basic/DiagnosticOptions.h"
Chandler Carruth320d9662012-12-04 09:45:34 +000013#include "clang/Basic/FileManager.h"
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000014#include "clang/Basic/LangOptions.h"
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000015#include "clang/Basic/TargetInfo.h"
Chandler Carruth320d9662012-12-04 09:45:34 +000016#include "clang/Basic/TargetOptions.h"
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000017#include "clang/Lex/HeaderSearch.h"
Douglas Gregor40ba1a02012-10-24 16:24:38 +000018#include "clang/Lex/HeaderSearchOptions.h"
Chandler Carruth320d9662012-12-04 09:45:34 +000019#include "clang/Lex/ModuleLoader.h"
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000020#include "clang/Lex/Preprocessor.h"
Douglas Gregor1452ff12012-10-24 17:46:57 +000021#include "clang/Lex/PreprocessorOptions.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000022#include "llvm/ADT/SmallString.h"
Alp Toker1d257e12014-06-04 03:28:55 +000023#include "llvm/Config/llvm-config.h"
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000024#include "gtest/gtest.h"
25
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000026using namespace clang;
27
28namespace {
29
30// The test fixture.
31class SourceManagerTest : public ::testing::Test {
32protected:
33 SourceManagerTest()
34 : FileMgr(FileMgrOpts),
35 DiagID(new DiagnosticIDs()),
Douglas Gregord8cfd392012-10-23 22:31:51 +000036 Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
Douglas Gregor44d63612012-10-17 00:11:35 +000037 SourceMgr(Diags, FileMgr),
38 TargetOpts(new TargetOptions) {
39 TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
Alp Toker80758082014-07-06 05:26:44 +000040 Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000041 }
42
43 FileSystemOptions FileMgrOpts;
44 FileManager FileMgr;
Dylan Noblesmithc95d8192012-02-20 14:00:23 +000045 IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000046 DiagnosticsEngine Diags;
47 SourceManager SourceMgr;
48 LangOptions LangOpts;
Alp Toker80758082014-07-06 05:26:44 +000049 std::shared_ptr<TargetOptions> TargetOpts;
Dylan Noblesmithc95d8192012-02-20 14:00:23 +000050 IntrusiveRefCntPtr<TargetInfo> Target;
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000051};
52
53class VoidModuleLoader : public ModuleLoader {
John Thompson2d94bbb2014-04-23 19:04:32 +000054 ModuleLoadResult loadModule(SourceLocation ImportLoc,
55 ModuleIdPath Path,
56 Module::NameVisibilityKind Visibility,
57 bool IsInclusionDirective) override {
Douglas Gregor8c058932012-11-30 00:01:57 +000058 return ModuleLoadResult();
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000059 }
NAKAMURA Takumie73d2a92013-01-12 02:16:29 +000060
John Thompson2d94bbb2014-04-23 19:04:32 +000061 void makeModuleVisible(Module *Mod,
62 Module::NameVisibilityKind Visibility,
Richard Smitha7e2cc62015-05-01 01:53:09 +000063 SourceLocation ImportLoc) override { }
John Thompson2255f2c2014-04-23 12:57:01 +000064
John Thompson2d94bbb2014-04-23 19:04:32 +000065 GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
Craig Topper416fa342014-06-08 08:38:12 +000066 { return nullptr; }
John Thompson2d94bbb2014-04-23 19:04:32 +000067 bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
Hans Wennborg4afe5042015-07-22 20:46:26 +000068 { return 0; }
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000069};
70
71TEST_F(SourceManagerTest, isBeforeInTranslationUnit) {
72 const char *source =
73 "#define M(x) [x]\n"
74 "M(foo)";
James Y Knightb214cbc2016-03-04 19:00:41 +000075 std::unique_ptr<llvm::MemoryBuffer> Buf =
76 llvm::MemoryBuffer::getMemBuffer(source);
David Blaikie50a5f972014-08-29 07:59:55 +000077 FileID mainFileID = SourceMgr.createFileID(std::move(Buf));
Alp Tokerb671e342014-05-21 01:12:41 +000078 SourceMgr.setMainFileID(mainFileID);
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000079
80 VoidModuleLoader ModLoader;
David Blaikie9c28cb32017-01-06 01:04:46 +000081 HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
82 Diags, LangOpts, &*Target);
David Blaikiee3041682017-01-05 19:11:36 +000083 Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
84 SourceMgr, HeaderInfo, ModLoader,
Craig Topper416fa342014-06-08 08:38:12 +000085 /*IILookup =*/nullptr,
Alp Toker1ae02f62014-05-02 03:43:30 +000086 /*OwnsHeaderSearch =*/false);
87 PP.Initialize(*Target);
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000088 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
Dylan Noblesmithf1a13f22012-02-13 12:32:26 +0000114 SmallString<32> str;
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +0000115 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
Jordan Rose8d63d5b2012-06-19 03:09:38 +0000124TEST_F(SourceManagerTest, getColumnNumber) {
125 const char *Source =
126 "int x;\n"
127 "int y;";
128
James Y Knightb214cbc2016-03-04 19:00:41 +0000129 std::unique_ptr<llvm::MemoryBuffer> Buf =
130 llvm::MemoryBuffer::getMemBuffer(Source);
David Blaikie50a5f972014-08-29 07:59:55 +0000131 FileID MainFileID = SourceMgr.createFileID(std::move(Buf));
Alp Tokerb671e342014-05-21 01:12:41 +0000132 SourceMgr.setMainFileID(MainFileID);
Jordan Rose8d63d5b2012-06-19 03:09:38 +0000133
134 bool Invalid;
135
136 Invalid = false;
137 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, &Invalid));
138 EXPECT_TRUE(!Invalid);
139
140 Invalid = false;
141 EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 4, &Invalid));
142 EXPECT_TRUE(!Invalid);
143
144 Invalid = false;
145 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 7, &Invalid));
146 EXPECT_TRUE(!Invalid);
147
148 Invalid = false;
149 EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 11, &Invalid));
150 EXPECT_TRUE(!Invalid);
151
152 Invalid = false;
153 EXPECT_EQ(7U, SourceMgr.getColumnNumber(MainFileID, strlen(Source),
154 &Invalid));
155 EXPECT_TRUE(!Invalid);
156
157 Invalid = false;
158 SourceMgr.getColumnNumber(MainFileID, strlen(Source)+1, &Invalid);
159 EXPECT_TRUE(Invalid);
160
161 // Test invalid files
162 Invalid = false;
163 SourceMgr.getColumnNumber(FileID(), 0, &Invalid);
164 EXPECT_TRUE(Invalid);
165
166 Invalid = false;
167 SourceMgr.getColumnNumber(FileID(), 1, &Invalid);
168 EXPECT_TRUE(Invalid);
169
170 // Test with no invalid flag.
Craig Topper416fa342014-06-08 08:38:12 +0000171 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, nullptr));
Jordan Rose8d63d5b2012-06-19 03:09:38 +0000172}
173
Argyrios Kyrtzidise841c902011-12-21 16:56:35 +0000174#if defined(LLVM_ON_UNIX)
175
176TEST_F(SourceManagerTest, getMacroArgExpandedLocation) {
177 const char *header =
178 "#define FM(x,y) x\n";
179
180 const char *main =
181 "#include \"/test-header.h\"\n"
182 "#define VAL 0\n"
183 "FM(VAL,0)\n"
184 "FM(0,VAL)\n"
185 "FM(FM(0,VAL),0)\n"
186 "#define CONCAT(X, Y) X##Y\n"
187 "CONCAT(1,1)\n";
188
James Y Knightb214cbc2016-03-04 19:00:41 +0000189 std::unique_ptr<llvm::MemoryBuffer> HeaderBuf =
190 llvm::MemoryBuffer::getMemBuffer(header);
191 std::unique_ptr<llvm::MemoryBuffer> MainBuf =
192 llvm::MemoryBuffer::getMemBuffer(main);
David Blaikie50a5f972014-08-29 07:59:55 +0000193 FileID mainFileID = SourceMgr.createFileID(std::move(MainBuf));
Alp Tokerb671e342014-05-21 01:12:41 +0000194 SourceMgr.setMainFileID(mainFileID);
Argyrios Kyrtzidise841c902011-12-21 16:56:35 +0000195
196 const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
Rafael Espindolad87f8d72014-08-27 20:03:29 +0000197 HeaderBuf->getBufferSize(), 0);
David Blaikie49cc3182014-08-27 20:54:45 +0000198 SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
Argyrios Kyrtzidise841c902011-12-21 16:56:35 +0000199
200 VoidModuleLoader ModLoader;
David Blaikie9c28cb32017-01-06 01:04:46 +0000201 HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
202 Diags, LangOpts, &*Target);
David Blaikiee3041682017-01-05 19:11:36 +0000203 Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
204 SourceMgr, HeaderInfo, ModLoader,
Craig Topper416fa342014-06-08 08:38:12 +0000205 /*IILookup =*/nullptr,
Alp Toker1ae02f62014-05-02 03:43:30 +0000206 /*OwnsHeaderSearch =*/false);
207 PP.Initialize(*Target);
Argyrios Kyrtzidise841c902011-12-21 16:56:35 +0000208 PP.EnterMainSourceFile();
209
210 std::vector<Token> toks;
211 while (1) {
212 Token tok;
213 PP.Lex(tok);
214 if (tok.is(tok::eof))
215 break;
216 toks.push_back(tok);
217 }
218
219 // Make sure we got the tokens that we expected.
220 ASSERT_EQ(4U, toks.size());
221 ASSERT_EQ(tok::numeric_constant, toks[0].getKind());
222 ASSERT_EQ(tok::numeric_constant, toks[1].getKind());
223 ASSERT_EQ(tok::numeric_constant, toks[2].getKind());
224 ASSERT_EQ(tok::numeric_constant, toks[3].getKind());
225
226 SourceLocation defLoc = SourceMgr.translateLineCol(mainFileID, 2, 13);
227 SourceLocation loc1 = SourceMgr.translateLineCol(mainFileID, 3, 8);
228 SourceLocation loc2 = SourceMgr.translateLineCol(mainFileID, 4, 4);
229 SourceLocation loc3 = SourceMgr.translateLineCol(mainFileID, 5, 7);
230 SourceLocation defLoc2 = SourceMgr.translateLineCol(mainFileID, 6, 22);
231 defLoc = SourceMgr.getMacroArgExpandedLocation(defLoc);
232 loc1 = SourceMgr.getMacroArgExpandedLocation(loc1);
233 loc2 = SourceMgr.getMacroArgExpandedLocation(loc2);
234 loc3 = SourceMgr.getMacroArgExpandedLocation(loc3);
235 defLoc2 = SourceMgr.getMacroArgExpandedLocation(defLoc2);
236
237 EXPECT_TRUE(defLoc.isFileID());
238 EXPECT_TRUE(loc1.isFileID());
239 EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc2));
240 EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc3));
241 EXPECT_EQ(loc2, toks[1].getLocation());
242 EXPECT_EQ(loc3, toks[2].getLocation());
243 EXPECT_TRUE(defLoc2.isFileID());
244}
245
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000246namespace {
247
248struct MacroAction {
249 SourceLocation Loc;
250 std::string Name;
251 bool isDefinition; // if false, it is expansion.
252
253 MacroAction(SourceLocation Loc, StringRef Name, bool isDefinition)
254 : Loc(Loc), Name(Name), isDefinition(isDefinition) { }
255};
256
257class MacroTracker : public PPCallbacks {
258 std::vector<MacroAction> &Macros;
259
260public:
261 explicit MacroTracker(std::vector<MacroAction> &Macros) : Macros(Macros) { }
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000262
263 void MacroDefined(const Token &MacroNameTok,
264 const MacroDirective *MD) override {
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000265 Macros.push_back(MacroAction(MD->getLocation(),
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000266 MacroNameTok.getIdentifierInfo()->getName(),
267 true));
268 }
Richard Smith36bd40d2015-05-04 03:15:40 +0000269 void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000270 SourceRange Range, const MacroArgs *Args) override {
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000271 Macros.push_back(MacroAction(MacroNameTok.getLocation(),
272 MacroNameTok.getIdentifierInfo()->getName(),
273 false));
274 }
275};
276
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000277}
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000278
279TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) {
280 const char *header =
281 "#define MACRO_IN_INCLUDE 0\n";
282
283 const char *main =
284 "#define M(x) x\n"
285 "#define INC \"/test-header.h\"\n"
286 "#include M(INC)\n"
287 "#define INC2 </test-header.h>\n"
288 "#include M(INC2)\n";
289
James Y Knightb214cbc2016-03-04 19:00:41 +0000290 std::unique_ptr<llvm::MemoryBuffer> HeaderBuf =
291 llvm::MemoryBuffer::getMemBuffer(header);
292 std::unique_ptr<llvm::MemoryBuffer> MainBuf =
293 llvm::MemoryBuffer::getMemBuffer(main);
David Blaikie50a5f972014-08-29 07:59:55 +0000294 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(MainBuf)));
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000295
296 const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
Rafael Espindolad87f8d72014-08-27 20:03:29 +0000297 HeaderBuf->getBufferSize(), 0);
David Blaikie49cc3182014-08-27 20:54:45 +0000298 SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000299
300 VoidModuleLoader ModLoader;
David Blaikie9c28cb32017-01-06 01:04:46 +0000301 HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
302 Diags, LangOpts, &*Target);
David Blaikiee3041682017-01-05 19:11:36 +0000303 Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
304 SourceMgr, HeaderInfo, ModLoader,
Craig Topper416fa342014-06-08 08:38:12 +0000305 /*IILookup =*/nullptr,
Alp Toker1ae02f62014-05-02 03:43:30 +0000306 /*OwnsHeaderSearch =*/false);
307 PP.Initialize(*Target);
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000308
309 std::vector<MacroAction> Macros;
Craig Topperb8a70532014-09-10 04:53:53 +0000310 PP.addPPCallbacks(llvm::make_unique<MacroTracker>(Macros));
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000311
312 PP.EnterMainSourceFile();
313
314 std::vector<Token> toks;
315 while (1) {
316 Token tok;
317 PP.Lex(tok);
318 if (tok.is(tok::eof))
319 break;
320 toks.push_back(tok);
321 }
322
323 // Make sure we got the tokens that we expected.
324 ASSERT_EQ(0U, toks.size());
325
326 ASSERT_EQ(9U, Macros.size());
327 // #define M(x) x
328 ASSERT_TRUE(Macros[0].isDefinition);
329 ASSERT_EQ("M", Macros[0].Name);
330 // #define INC "/test-header.h"
331 ASSERT_TRUE(Macros[1].isDefinition);
332 ASSERT_EQ("INC", Macros[1].Name);
333 // M expansion in #include M(INC)
334 ASSERT_FALSE(Macros[2].isDefinition);
335 ASSERT_EQ("M", Macros[2].Name);
336 // INC expansion in #include M(INC)
337 ASSERT_FALSE(Macros[3].isDefinition);
338 ASSERT_EQ("INC", Macros[3].Name);
339 // #define MACRO_IN_INCLUDE 0
340 ASSERT_TRUE(Macros[4].isDefinition);
341 ASSERT_EQ("MACRO_IN_INCLUDE", Macros[4].Name);
342 // #define INC2 </test-header.h>
343 ASSERT_TRUE(Macros[5].isDefinition);
344 ASSERT_EQ("INC2", Macros[5].Name);
345 // M expansion in #include M(INC2)
346 ASSERT_FALSE(Macros[6].isDefinition);
347 ASSERT_EQ("M", Macros[6].Name);
348 // INC2 expansion in #include M(INC2)
349 ASSERT_FALSE(Macros[7].isDefinition);
350 ASSERT_EQ("INC2", Macros[7].Name);
351 // #define MACRO_IN_INCLUDE 0
352 ASSERT_TRUE(Macros[8].isDefinition);
353 ASSERT_EQ("MACRO_IN_INCLUDE", Macros[8].Name);
354
355 // The INC expansion in #include M(INC) comes before the first
356 // MACRO_IN_INCLUDE definition of the included file.
357 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[3].Loc, Macros[4].Loc));
358
359 // The INC2 expansion in #include M(INC2) comes before the second
360 // MACRO_IN_INCLUDE definition of the included file.
361 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[7].Loc, Macros[8].Loc));
362}
363
Argyrios Kyrtzidise841c902011-12-21 16:56:35 +0000364#endif
365
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +0000366} // anonymous namespace