1 //===- unittests/Basic/SourceManagerTest.cpp ------ SourceManager tests ---===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "clang/Basic/SourceManager.h"
10 #include "clang/Basic/Diagnostic.h"
11 #include "clang/Basic/DiagnosticOptions.h"
12 #include "clang/Basic/FileManager.h"
13 #include "clang/Basic/LangOptions.h"
14 #include "clang/Basic/TargetInfo.h"
15 #include "clang/Basic/TargetOptions.h"
16 #include "clang/Lex/HeaderSearch.h"
17 #include "clang/Lex/HeaderSearchOptions.h"
18 #include "clang/Lex/ModuleLoader.h"
19 #include "clang/Lex/Preprocessor.h"
20 #include "clang/Lex/PreprocessorOptions.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/Config/llvm-config.h"
23 #include "llvm/Support/Process.h"
24 #include "gtest/gtest.h"
27 using namespace clang
;
32 class SourceManagerTest
: public ::testing::Test
{
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
);
44 FileSystemOptions FileMgrOpts
;
46 IntrusiveRefCntPtr
<DiagnosticIDs
> DiagID
;
47 DiagnosticsEngine Diags
;
48 SourceManager SourceMgr
;
50 std::shared_ptr
<TargetOptions
> TargetOpts
;
51 IntrusiveRefCntPtr
<TargetInfo
> Target
;
54 TEST_F(SourceManagerTest
, isBeforeInTranslationUnit
) {
58 std::unique_ptr
<llvm::MemoryBuffer
> Buf
=
59 llvm::MemoryBuffer::getMemBuffer(source
);
60 FileID mainFileID
= SourceMgr
.createFileID(std::move(Buf
));
61 SourceMgr
.setMainFileID(mainFileID
);
63 TrivialModuleLoader ModLoader
;
64 HeaderSearch
HeaderInfo(std::make_shared
<HeaderSearchOptions
>(), SourceMgr
,
65 Diags
, LangOpts
, &*Target
);
66 Preprocessor
PP(std::make_shared
<PreprocessorOptions
>(), Diags
, LangOpts
,
67 SourceMgr
, HeaderInfo
, ModLoader
,
68 /*IILookup =*/nullptr,
69 /*OwnsHeaderSearch =*/false);
70 PP
.Initialize(*Target
);
71 PP
.EnterMainSourceFile();
73 std::vector
<Token
> toks
;
82 // Make sure we got the tokens that we expected.
83 ASSERT_EQ(3U, toks
.size());
84 ASSERT_EQ(tok::l_square
, toks
[0].getKind());
85 ASSERT_EQ(tok::identifier
, toks
[1].getKind());
86 ASSERT_EQ(tok::r_square
, toks
[2].getKind());
88 SourceLocation lsqrLoc
= toks
[0].getLocation();
89 SourceLocation idLoc
= toks
[1].getLocation();
90 SourceLocation rsqrLoc
= toks
[2].getLocation();
92 SourceLocation macroExpStartLoc
= SourceMgr
.translateLineCol(mainFileID
, 2, 1);
93 SourceLocation macroExpEndLoc
= SourceMgr
.translateLineCol(mainFileID
, 2, 6);
94 ASSERT_TRUE(macroExpStartLoc
.isFileID());
95 ASSERT_TRUE(macroExpEndLoc
.isFileID());
98 ASSERT_EQ("M", PP
.getSpelling(macroExpStartLoc
, str
));
99 ASSERT_EQ(")", PP
.getSpelling(macroExpEndLoc
, str
));
101 EXPECT_TRUE(SourceMgr
.isBeforeInTranslationUnit(lsqrLoc
, idLoc
));
102 EXPECT_TRUE(SourceMgr
.isBeforeInTranslationUnit(idLoc
, rsqrLoc
));
103 EXPECT_TRUE(SourceMgr
.isBeforeInTranslationUnit(macroExpStartLoc
, idLoc
));
104 EXPECT_TRUE(SourceMgr
.isBeforeInTranslationUnit(idLoc
, macroExpEndLoc
));
107 TEST_F(SourceManagerTest
, getColumnNumber
) {
112 std::unique_ptr
<llvm::MemoryBuffer
> Buf
=
113 llvm::MemoryBuffer::getMemBuffer(Source
);
114 FileID MainFileID
= SourceMgr
.createFileID(std::move(Buf
));
115 SourceMgr
.setMainFileID(MainFileID
);
120 EXPECT_EQ(1U, SourceMgr
.getColumnNumber(MainFileID
, 0, &Invalid
));
121 EXPECT_TRUE(!Invalid
);
124 EXPECT_EQ(5U, SourceMgr
.getColumnNumber(MainFileID
, 4, &Invalid
));
125 EXPECT_TRUE(!Invalid
);
128 EXPECT_EQ(1U, SourceMgr
.getColumnNumber(MainFileID
, 7, &Invalid
));
129 EXPECT_TRUE(!Invalid
);
132 EXPECT_EQ(5U, SourceMgr
.getColumnNumber(MainFileID
, 11, &Invalid
));
133 EXPECT_TRUE(!Invalid
);
136 EXPECT_EQ(7U, SourceMgr
.getColumnNumber(MainFileID
, strlen(Source
),
138 EXPECT_TRUE(!Invalid
);
141 SourceMgr
.getColumnNumber(MainFileID
, strlen(Source
)+1, &Invalid
);
142 EXPECT_TRUE(Invalid
);
144 // Test invalid files
146 SourceMgr
.getColumnNumber(FileID(), 0, &Invalid
);
147 EXPECT_TRUE(Invalid
);
150 SourceMgr
.getColumnNumber(FileID(), 1, &Invalid
);
151 EXPECT_TRUE(Invalid
);
153 // Test with no invalid flag.
154 EXPECT_EQ(1U, SourceMgr
.getColumnNumber(MainFileID
, 0, nullptr));
157 TEST_F(SourceManagerTest
, locationPrintTest
) {
158 const char *header
= "#define IDENTITY(x) x\n";
160 const char *Source
= "int x;\n"
161 "include \"test-header.h\"\n"
165 std::unique_ptr
<llvm::MemoryBuffer
> HeaderBuf
=
166 llvm::MemoryBuffer::getMemBuffer(header
);
167 std::unique_ptr
<llvm::MemoryBuffer
> Buf
=
168 llvm::MemoryBuffer::getMemBuffer(Source
);
170 const FileEntry
*SourceFile
=
171 FileMgr
.getVirtualFile("/mainFile.cpp", Buf
->getBufferSize(), 0);
172 SourceMgr
.overrideFileContents(SourceFile
, std::move(Buf
));
174 const FileEntry
*HeaderFile
=
175 FileMgr
.getVirtualFile("/test-header.h", HeaderBuf
->getBufferSize(), 0);
176 SourceMgr
.overrideFileContents(HeaderFile
, std::move(HeaderBuf
));
178 FileID MainFileID
= SourceMgr
.getOrCreateFileID(SourceFile
, SrcMgr::C_User
);
179 FileID HeaderFileID
= SourceMgr
.getOrCreateFileID(HeaderFile
, SrcMgr::C_User
);
180 SourceMgr
.setMainFileID(MainFileID
);
182 auto BeginLoc
= SourceMgr
.getLocForStartOfFile(MainFileID
);
183 auto EndLoc
= SourceMgr
.getLocForEndOfFile(MainFileID
);
185 auto BeginEOLLoc
= SourceMgr
.translateLineCol(MainFileID
, 1, 7);
187 auto HeaderLoc
= SourceMgr
.getLocForStartOfFile(HeaderFileID
);
189 EXPECT_EQ(BeginLoc
.printToString(SourceMgr
), "/mainFile.cpp:1:1");
190 EXPECT_EQ(EndLoc
.printToString(SourceMgr
), "/mainFile.cpp:4:7");
192 EXPECT_EQ(BeginEOLLoc
.printToString(SourceMgr
), "/mainFile.cpp:1:7");
193 EXPECT_EQ(HeaderLoc
.printToString(SourceMgr
), "/test-header.h:1:1");
195 EXPECT_EQ(SourceRange(BeginLoc
, BeginLoc
).printToString(SourceMgr
),
196 "</mainFile.cpp:1:1>");
197 EXPECT_EQ(SourceRange(BeginLoc
, BeginEOLLoc
).printToString(SourceMgr
),
198 "</mainFile.cpp:1:1, col:7>");
199 EXPECT_EQ(SourceRange(BeginLoc
, EndLoc
).printToString(SourceMgr
),
200 "</mainFile.cpp:1:1, line:4:7>");
201 EXPECT_EQ(SourceRange(BeginLoc
, HeaderLoc
).printToString(SourceMgr
),
202 "</mainFile.cpp:1:1, /test-header.h:1:1>");
205 TEST_F(SourceManagerTest
, getInvalidBOM
) {
206 ASSERT_EQ(SrcMgr::ContentCache::getInvalidBOM(""), nullptr);
207 ASSERT_EQ(SrcMgr::ContentCache::getInvalidBOM("\x00\x00\x00"), nullptr);
208 ASSERT_EQ(SrcMgr::ContentCache::getInvalidBOM("\xFF\xFF\xFF"), nullptr);
209 ASSERT_EQ(SrcMgr::ContentCache::getInvalidBOM("#include <iostream>"),
212 ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM(
213 "\xFE\xFF#include <iostream>")),
215 ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM(
216 "\xFF\xFE#include <iostream>")),
218 ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM(
219 "\x2B\x2F\x76#include <iostream>")),
221 ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM(
222 "\xF7\x64\x4C#include <iostream>")),
224 ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM(
225 "\xDD\x73\x66\x73#include <iostream>")),
227 ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM(
228 "\x0E\xFE\xFF#include <iostream>")),
230 ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM(
231 "\xFB\xEE\x28#include <iostream>")),
233 ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM(
234 "\x84\x31\x95\x33#include <iostream>")),
236 ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM(
237 llvm::StringLiteral::withInnerNUL(
238 "\x00\x00\xFE\xFF#include <iostream>"))),
240 ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM(
241 llvm::StringLiteral::withInnerNUL(
242 "\xFF\xFE\x00\x00#include <iostream>"))),
246 // Regression test - there was an out of bound access for buffers not terminated by zero.
247 TEST_F(SourceManagerTest
, getLineNumber
) {
248 const unsigned pageSize
= llvm::sys::Process::getPageSizeEstimate();
249 std::unique_ptr
<char[]> source(new char[pageSize
]);
250 for(unsigned i
= 0; i
< pageSize
; ++i
) {
254 std::unique_ptr
<llvm::MemoryBuffer
> Buf
=
255 llvm::MemoryBuffer::getMemBuffer(
256 llvm::MemoryBufferRef(
257 llvm::StringRef(source
.get(), 3), "whatever"
262 FileID mainFileID
= SourceMgr
.createFileID(std::move(Buf
));
263 SourceMgr
.setMainFileID(mainFileID
);
265 ASSERT_NO_FATAL_FAILURE(SourceMgr
.getLineNumber(mainFileID
, 1, nullptr));
268 #if defined(LLVM_ON_UNIX)
270 TEST_F(SourceManagerTest
, getMacroArgExpandedLocation
) {
272 "#define FM(x,y) x\n";
275 "#include \"/test-header.h\"\n"
280 "#define CONCAT(X, Y) X##Y\n"
283 std::unique_ptr
<llvm::MemoryBuffer
> HeaderBuf
=
284 llvm::MemoryBuffer::getMemBuffer(header
);
285 std::unique_ptr
<llvm::MemoryBuffer
> MainBuf
=
286 llvm::MemoryBuffer::getMemBuffer(main
);
287 FileID mainFileID
= SourceMgr
.createFileID(std::move(MainBuf
));
288 SourceMgr
.setMainFileID(mainFileID
);
290 const FileEntry
*headerFile
= FileMgr
.getVirtualFile("/test-header.h",
291 HeaderBuf
->getBufferSize(), 0);
292 SourceMgr
.overrideFileContents(headerFile
, std::move(HeaderBuf
));
294 TrivialModuleLoader ModLoader
;
295 HeaderSearch
HeaderInfo(std::make_shared
<HeaderSearchOptions
>(), SourceMgr
,
296 Diags
, LangOpts
, &*Target
);
298 Preprocessor
PP(std::make_shared
<PreprocessorOptions
>(), Diags
, LangOpts
,
299 SourceMgr
, HeaderInfo
, ModLoader
,
300 /*IILookup =*/nullptr,
301 /*OwnsHeaderSearch =*/false);
302 // Ensure we can get expanded locations in presence of implicit includes.
303 // These are different than normal includes since predefines buffer doesn't
304 // have a valid insertion location.
305 PP
.setPredefines("#include \"/implicit-header.h\"");
306 FileMgr
.getVirtualFile("/implicit-header.h", 0, 0);
307 PP
.Initialize(*Target
);
308 PP
.EnterMainSourceFile();
310 std::vector
<Token
> toks
;
314 if (tok
.is(tok::eof
))
319 // Make sure we got the tokens that we expected.
320 ASSERT_EQ(4U, toks
.size());
321 ASSERT_EQ(tok::numeric_constant
, toks
[0].getKind());
322 ASSERT_EQ(tok::numeric_constant
, toks
[1].getKind());
323 ASSERT_EQ(tok::numeric_constant
, toks
[2].getKind());
324 ASSERT_EQ(tok::numeric_constant
, toks
[3].getKind());
326 SourceLocation defLoc
= SourceMgr
.translateLineCol(mainFileID
, 2, 13);
327 SourceLocation loc1
= SourceMgr
.translateLineCol(mainFileID
, 3, 8);
328 SourceLocation loc2
= SourceMgr
.translateLineCol(mainFileID
, 4, 4);
329 SourceLocation loc3
= SourceMgr
.translateLineCol(mainFileID
, 5, 7);
330 SourceLocation defLoc2
= SourceMgr
.translateLineCol(mainFileID
, 6, 22);
331 defLoc
= SourceMgr
.getMacroArgExpandedLocation(defLoc
);
332 loc1
= SourceMgr
.getMacroArgExpandedLocation(loc1
);
333 loc2
= SourceMgr
.getMacroArgExpandedLocation(loc2
);
334 loc3
= SourceMgr
.getMacroArgExpandedLocation(loc3
);
335 defLoc2
= SourceMgr
.getMacroArgExpandedLocation(defLoc2
);
337 EXPECT_TRUE(defLoc
.isFileID());
338 EXPECT_TRUE(loc1
.isFileID());
339 EXPECT_TRUE(SourceMgr
.isMacroArgExpansion(loc2
));
340 EXPECT_TRUE(SourceMgr
.isMacroArgExpansion(loc3
));
341 EXPECT_EQ(loc2
, toks
[1].getLocation());
342 EXPECT_EQ(loc3
, toks
[2].getLocation());
343 EXPECT_TRUE(defLoc2
.isFileID());
349 enum Kind
{ kExpansion
, kDefinition
, kUnDefinition
};
355 MacroAction(SourceLocation Loc
, StringRef Name
, unsigned K
)
356 : Loc(Loc
), Name(std::string(Name
)), MAKind(K
) {}
358 bool isExpansion() const { return MAKind
== kExpansion
; }
359 bool isDefinition() const { return MAKind
& kDefinition
; }
360 bool isUnDefinition() const { return MAKind
& kUnDefinition
; }
363 class MacroTracker
: public PPCallbacks
{
364 std::vector
<MacroAction
> &Macros
;
367 explicit MacroTracker(std::vector
<MacroAction
> &Macros
) : Macros(Macros
) { }
369 void MacroDefined(const Token
&MacroNameTok
,
370 const MacroDirective
*MD
) override
{
371 Macros
.push_back(MacroAction(MD
->getLocation(),
372 MacroNameTok
.getIdentifierInfo()->getName(),
373 MacroAction::kDefinition
));
375 void MacroUndefined(const Token
&MacroNameTok
,
376 const MacroDefinition
&MD
,
377 const MacroDirective
*UD
) override
{
379 MacroAction(UD
? UD
->getLocation() : SourceLocation(),
380 MacroNameTok
.getIdentifierInfo()->getName(),
381 UD
? MacroAction::kDefinition
| MacroAction::kUnDefinition
382 : MacroAction::kUnDefinition
));
384 void MacroExpands(const Token
&MacroNameTok
, const MacroDefinition
&MD
,
385 SourceRange Range
, const MacroArgs
*Args
) override
{
386 Macros
.push_back(MacroAction(MacroNameTok
.getLocation(),
387 MacroNameTok
.getIdentifierInfo()->getName(),
388 MacroAction::kExpansion
));
394 TEST_F(SourceManagerTest
, isBeforeInTranslationUnitWithMacroInInclude
) {
396 "#define MACRO_IN_INCLUDE 0\n"
397 "#define MACRO_DEFINED\n"
398 "#undef MACRO_DEFINED\n"
399 "#undef MACRO_UNDEFINED\n";
403 "#define INC \"/test-header.h\"\n"
405 "#define INC2 </test-header.h>\n"
406 "#include M(INC2)\n";
408 std::unique_ptr
<llvm::MemoryBuffer
> HeaderBuf
=
409 llvm::MemoryBuffer::getMemBuffer(header
);
410 std::unique_ptr
<llvm::MemoryBuffer
> MainBuf
=
411 llvm::MemoryBuffer::getMemBuffer(main
);
412 SourceMgr
.setMainFileID(SourceMgr
.createFileID(std::move(MainBuf
)));
414 const FileEntry
*headerFile
= FileMgr
.getVirtualFile("/test-header.h",
415 HeaderBuf
->getBufferSize(), 0);
416 SourceMgr
.overrideFileContents(headerFile
, std::move(HeaderBuf
));
418 TrivialModuleLoader ModLoader
;
419 HeaderSearch
HeaderInfo(std::make_shared
<HeaderSearchOptions
>(), SourceMgr
,
420 Diags
, LangOpts
, &*Target
);
421 Preprocessor
PP(std::make_shared
<PreprocessorOptions
>(), Diags
, LangOpts
,
422 SourceMgr
, HeaderInfo
, ModLoader
,
423 /*IILookup =*/nullptr,
424 /*OwnsHeaderSearch =*/false);
425 PP
.Initialize(*Target
);
427 std::vector
<MacroAction
> Macros
;
428 PP
.addPPCallbacks(std::make_unique
<MacroTracker
>(Macros
));
430 PP
.EnterMainSourceFile();
432 std::vector
<Token
> toks
;
436 if (tok
.is(tok::eof
))
441 // Make sure we got the tokens that we expected.
442 ASSERT_EQ(0U, toks
.size());
444 ASSERT_EQ(15U, Macros
.size());
446 ASSERT_TRUE(Macros
[0].isDefinition());
447 ASSERT_EQ("M", Macros
[0].Name
);
448 // #define INC "/test-header.h"
449 ASSERT_TRUE(Macros
[1].isDefinition());
450 ASSERT_EQ("INC", Macros
[1].Name
);
451 // M expansion in #include M(INC)
452 ASSERT_FALSE(Macros
[2].isDefinition());
453 ASSERT_EQ("M", Macros
[2].Name
);
454 // INC expansion in #include M(INC)
455 ASSERT_TRUE(Macros
[3].isExpansion());
456 ASSERT_EQ("INC", Macros
[3].Name
);
457 // #define MACRO_IN_INCLUDE 0
458 ASSERT_TRUE(Macros
[4].isDefinition());
459 ASSERT_EQ("MACRO_IN_INCLUDE", Macros
[4].Name
);
460 // #define MACRO_DEFINED
461 ASSERT_TRUE(Macros
[5].isDefinition());
462 ASSERT_FALSE(Macros
[5].isUnDefinition());
463 ASSERT_EQ("MACRO_DEFINED", Macros
[5].Name
);
464 // #undef MACRO_DEFINED
465 ASSERT_TRUE(Macros
[6].isDefinition());
466 ASSERT_TRUE(Macros
[6].isUnDefinition());
467 ASSERT_EQ("MACRO_DEFINED", Macros
[6].Name
);
468 // #undef MACRO_UNDEFINED
469 ASSERT_FALSE(Macros
[7].isDefinition());
470 ASSERT_TRUE(Macros
[7].isUnDefinition());
471 ASSERT_EQ("MACRO_UNDEFINED", Macros
[7].Name
);
472 // #define INC2 </test-header.h>
473 ASSERT_TRUE(Macros
[8].isDefinition());
474 ASSERT_EQ("INC2", Macros
[8].Name
);
475 // M expansion in #include M(INC2)
476 ASSERT_FALSE(Macros
[9].isDefinition());
477 ASSERT_EQ("M", Macros
[9].Name
);
478 // INC2 expansion in #include M(INC2)
479 ASSERT_TRUE(Macros
[10].isExpansion());
480 ASSERT_EQ("INC2", Macros
[10].Name
);
481 // #define MACRO_IN_INCLUDE 0
482 ASSERT_TRUE(Macros
[11].isDefinition());
483 ASSERT_EQ("MACRO_IN_INCLUDE", Macros
[11].Name
);
485 // The INC expansion in #include M(INC) comes before the first
486 // MACRO_IN_INCLUDE definition of the included file.
487 EXPECT_TRUE(SourceMgr
.isBeforeInTranslationUnit(Macros
[3].Loc
, Macros
[4].Loc
));
489 // The INC2 expansion in #include M(INC2) comes before the second
490 // MACRO_IN_INCLUDE definition of the included file.
491 EXPECT_TRUE(SourceMgr
.isBeforeInTranslationUnit(Macros
[10].Loc
, Macros
[11].Loc
));
494 TEST_F(SourceManagerTest
, isMainFile
) {
495 const char *Source
= "int x;";
497 std::unique_ptr
<llvm::MemoryBuffer
> Buf
=
498 llvm::MemoryBuffer::getMemBuffer(Source
);
499 const FileEntry
*SourceFile
=
500 FileMgr
.getVirtualFile("mainFile.cpp", Buf
->getBufferSize(), 0);
501 SourceMgr
.overrideFileContents(SourceFile
, std::move(Buf
));
503 std::unique_ptr
<llvm::MemoryBuffer
> Buf2
=
504 llvm::MemoryBuffer::getMemBuffer(Source
);
505 const FileEntry
*SecondFile
=
506 FileMgr
.getVirtualFile("file2.cpp", Buf2
->getBufferSize(), 0);
507 SourceMgr
.overrideFileContents(SecondFile
, std::move(Buf2
));
509 FileID MainFileID
= SourceMgr
.getOrCreateFileID(SourceFile
, SrcMgr::C_User
);
510 SourceMgr
.setMainFileID(MainFileID
);
512 EXPECT_TRUE(SourceMgr
.isMainFile(*SourceFile
));
513 EXPECT_TRUE(SourceMgr
.isMainFile(*SourceFile
));
514 EXPECT_FALSE(SourceMgr
.isMainFile(*SecondFile
));
519 } // anonymous namespace