blob: 5a1a393fbf02abdba1a6e7030ce7b344a2bc6393 [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
26using namespace llvm;
27using namespace clang;
28
29namespace {
30
31// The test fixture.
32class SourceManagerTest : public ::testing::Test {
33protected:
34 SourceManagerTest()
35 : FileMgr(FileMgrOpts),
36 DiagID(new DiagnosticIDs()),
Douglas Gregord8cfd392012-10-23 22:31:51 +000037 Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
Douglas Gregor44d63612012-10-17 00:11:35 +000038 SourceMgr(Diags, FileMgr),
39 TargetOpts(new TargetOptions) {
40 TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
Alp Toker80758082014-07-06 05:26:44 +000041 Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000042 }
43
44 FileSystemOptions FileMgrOpts;
45 FileManager FileMgr;
Dylan Noblesmithc95d8192012-02-20 14:00:23 +000046 IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000047 DiagnosticsEngine Diags;
48 SourceManager SourceMgr;
49 LangOptions LangOpts;
Alp Toker80758082014-07-06 05:26:44 +000050 std::shared_ptr<TargetOptions> TargetOpts;
Dylan Noblesmithc95d8192012-02-20 14:00:23 +000051 IntrusiveRefCntPtr<TargetInfo> Target;
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000052};
53
54class VoidModuleLoader : public ModuleLoader {
John Thompson2d94bbb2014-04-23 19:04:32 +000055 ModuleLoadResult loadModule(SourceLocation ImportLoc,
56 ModuleIdPath Path,
57 Module::NameVisibilityKind Visibility,
58 bool IsInclusionDirective) override {
Douglas Gregor8c058932012-11-30 00:01:57 +000059 return ModuleLoadResult();
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000060 }
NAKAMURA Takumie73d2a92013-01-12 02:16:29 +000061
John Thompson2d94bbb2014-04-23 19:04:32 +000062 void makeModuleVisible(Module *Mod,
63 Module::NameVisibilityKind Visibility,
Richard Smitha7e2cc62015-05-01 01:53:09 +000064 SourceLocation ImportLoc) override { }
John Thompson2255f2c2014-04-23 12:57:01 +000065
John Thompson2d94bbb2014-04-23 19:04:32 +000066 GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
Craig Topper416fa342014-06-08 08:38:12 +000067 { return nullptr; }
John Thompson2d94bbb2014-04-23 19:04:32 +000068 bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
Hans Wennborg4afe5042015-07-22 20:46:26 +000069 { return 0; }
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +000070};
71
72TEST_F(SourceManagerTest, isBeforeInTranslationUnit) {
73 const char *source =
74 "#define M(x) [x]\n"
75 "M(foo)";
Rafael Espindolad87f8d72014-08-27 20:03:29 +000076 std::unique_ptr<MemoryBuffer> Buf = 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;
Manuel Klimek1f76c4e2013-10-24 07:51:24 +000081 HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts,
Douglas Gregor40ba1a02012-10-24 16:24:38 +000082 &*Target);
Alp Toker96637802014-05-02 03:43:38 +000083 Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, SourceMgr,
84 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
Rafael Espindolad87f8d72014-08-27 20:03:29 +0000129 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Source);
David Blaikie50a5f972014-08-29 07:59:55 +0000130 FileID MainFileID = SourceMgr.createFileID(std::move(Buf));
Alp Tokerb671e342014-05-21 01:12:41 +0000131 SourceMgr.setMainFileID(MainFileID);
Jordan Rose8d63d5b2012-06-19 03:09:38 +0000132
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.
Craig Topper416fa342014-06-08 08:38:12 +0000170 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, nullptr));
Jordan Rose8d63d5b2012-06-19 03:09:38 +0000171}
172
Argyrios Kyrtzidise841c902011-12-21 16:56:35 +0000173#if defined(LLVM_ON_UNIX)
174
175TEST_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
Rafael Espindolad87f8d72014-08-27 20:03:29 +0000188 std::unique_ptr<MemoryBuffer> HeaderBuf = MemoryBuffer::getMemBuffer(header);
189 std::unique_ptr<MemoryBuffer> MainBuf = MemoryBuffer::getMemBuffer(main);
David Blaikie50a5f972014-08-29 07:59:55 +0000190 FileID mainFileID = SourceMgr.createFileID(std::move(MainBuf));
Alp Tokerb671e342014-05-21 01:12:41 +0000191 SourceMgr.setMainFileID(mainFileID);
Argyrios Kyrtzidise841c902011-12-21 16:56:35 +0000192
193 const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
Rafael Espindolad87f8d72014-08-27 20:03:29 +0000194 HeaderBuf->getBufferSize(), 0);
David Blaikie49cc3182014-08-27 20:54:45 +0000195 SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
Argyrios Kyrtzidise841c902011-12-21 16:56:35 +0000196
197 VoidModuleLoader ModLoader;
Manuel Klimek1f76c4e2013-10-24 07:51:24 +0000198 HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts,
Douglas Gregor40ba1a02012-10-24 16:24:38 +0000199 &*Target);
Alp Toker96637802014-05-02 03:43:38 +0000200 Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, SourceMgr,
201 HeaderInfo, ModLoader,
Craig Topper416fa342014-06-08 08:38:12 +0000202 /*IILookup =*/nullptr,
Alp Toker1ae02f62014-05-02 03:43:30 +0000203 /*OwnsHeaderSearch =*/false);
204 PP.Initialize(*Target);
Argyrios Kyrtzidise841c902011-12-21 16:56:35 +0000205 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
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000243namespace {
244
245struct 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
254class MacroTracker : public PPCallbacks {
255 std::vector<MacroAction> &Macros;
256
257public:
258 explicit MacroTracker(std::vector<MacroAction> &Macros) : Macros(Macros) { }
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000259
260 void MacroDefined(const Token &MacroNameTok,
261 const MacroDirective *MD) override {
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000262 Macros.push_back(MacroAction(MD->getLocation(),
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000263 MacroNameTok.getIdentifierInfo()->getName(),
264 true));
265 }
Richard Smith36bd40d2015-05-04 03:15:40 +0000266 void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
Alexander Kornienko34eb2072015-04-11 02:00:23 +0000267 SourceRange Range, const MacroArgs *Args) override {
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000268 Macros.push_back(MacroAction(MacroNameTok.getLocation(),
269 MacroNameTok.getIdentifierInfo()->getName(),
270 false));
271 }
272};
273
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000274}
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000275
276TEST_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
Rafael Espindolad87f8d72014-08-27 20:03:29 +0000287 std::unique_ptr<MemoryBuffer> HeaderBuf = MemoryBuffer::getMemBuffer(header);
288 std::unique_ptr<MemoryBuffer> MainBuf = MemoryBuffer::getMemBuffer(main);
David Blaikie50a5f972014-08-29 07:59:55 +0000289 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(MainBuf)));
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000290
291 const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
Rafael Espindolad87f8d72014-08-27 20:03:29 +0000292 HeaderBuf->getBufferSize(), 0);
David Blaikie49cc3182014-08-27 20:54:45 +0000293 SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000294
295 VoidModuleLoader ModLoader;
Manuel Klimek1f76c4e2013-10-24 07:51:24 +0000296 HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts,
Douglas Gregor40ba1a02012-10-24 16:24:38 +0000297 &*Target);
Alp Toker96637802014-05-02 03:43:38 +0000298 Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, SourceMgr,
299 HeaderInfo, ModLoader,
Craig Topper416fa342014-06-08 08:38:12 +0000300 /*IILookup =*/nullptr,
Alp Toker1ae02f62014-05-02 03:43:30 +0000301 /*OwnsHeaderSearch =*/false);
302 PP.Initialize(*Target);
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000303
304 std::vector<MacroAction> Macros;
Craig Topperb8a70532014-09-10 04:53:53 +0000305 PP.addPPCallbacks(llvm::make_unique<MacroTracker>(Macros));
Argyrios Kyrtzidisa9564502012-03-27 18:47:48 +0000306
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
Argyrios Kyrtzidise841c902011-12-21 16:56:35 +0000359#endif
360
Argyrios Kyrtzidis24037972011-12-21 16:56:29 +0000361} // anonymous namespace