| 1 |
//===- unittests/Basic/SourceManagerTest.cpp ------ SourceManager tests ---===// |
| 2 |
// |
| 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 |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 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/MemoryBuffer.h" |
| 24 |
#include "llvm/Support/Process.h" |
| 25 |
#include "gmock/gmock.h" |
| 26 |
#include "gtest/gtest.h" |
| 27 |
#include <cstddef> |
| 28 |
|
| 29 |
using namespace clang; |
| 30 |
using ::testing::IsEmpty; |
| 31 |
|
| 32 |
namespace clang { |
| 33 |
class SourceManagerTestHelper { |
| 34 |
public: |
| 35 |
static FileID makeFileID(int ID) { return FileID::get(ID); } |
| 36 |
}; |
| 37 |
} // namespace clang |
| 38 |
|
| 39 |
namespace { |
| 40 |
|
| 41 |
// The test fixture. |
| 42 |
class SourceManagerTest : public ::testing::Test { |
| 43 |
protected: |
| 44 |
SourceManagerTest() |
| 45 |
: FileMgr(FileMgrOpts), |
| 46 |
Diags(DiagnosticIDs::create(), DiagOpts, new IgnoringDiagConsumer()), |
| 47 |
SourceMgr(Diags, FileMgr), TargetOpts(new TargetOptions) { |
| 48 |
TargetOpts->Triple = "x86_64-apple-darwin11.1.0"; |
| 49 |
Target = TargetInfo::CreateTargetInfo(Diags, *TargetOpts); |
| 50 |
} |
| 51 |
|
| 52 |
FileSystemOptions FileMgrOpts; |
| 53 |
FileManager FileMgr; |
| 54 |
DiagnosticOptions DiagOpts; |
| 55 |
DiagnosticsEngine Diags; |
| 56 |
SourceManager SourceMgr; |
| 57 |
LangOptions LangOpts; |
| 58 |
std::shared_ptr<TargetOptions> TargetOpts; |
| 59 |
IntrusiveRefCntPtr<TargetInfo> Target; |
| 60 |
}; |
| 61 |
|
| 62 |
TEST_F(SourceManagerTest, isInMemoryBuffersNoSourceLocationInfo) { |
| 63 |
// Check for invalid source location for each method |
| 64 |
SourceLocation LocEmpty; |
| 65 |
bool isWrittenInBuiltInFileFalse = SourceMgr.isWrittenInBuiltinFile(LocEmpty); |
| 66 |
bool isWrittenInCommandLineFileFalse = |
| 67 |
SourceMgr.isWrittenInCommandLineFile(LocEmpty); |
| 68 |
bool isWrittenInScratchSpaceFalse = |
| 69 |
SourceMgr.isWrittenInScratchSpace(LocEmpty); |
| 70 |
|
| 71 |
EXPECT_FALSE(isWrittenInBuiltInFileFalse); |
| 72 |
EXPECT_FALSE(isWrittenInCommandLineFileFalse); |
| 73 |
EXPECT_FALSE(isWrittenInScratchSpaceFalse); |
| 74 |
|
| 75 |
// Check for valid source location per filename for each method |
| 76 |
const char *Source = "int x"; |
| 77 |
|
| 78 |
std::unique_ptr<llvm::MemoryBuffer> BuiltInBuf = |
| 79 |
llvm::MemoryBuffer::getMemBuffer(Source); |
| 80 |
FileEntryRef BuiltInFile = |
| 81 |
FileMgr.getVirtualFileRef("<built-in>", BuiltInBuf->getBufferSize(), 0); |
| 82 |
SourceMgr.overrideFileContents(BuiltInFile, std::move(BuiltInBuf)); |
| 83 |
FileID BuiltInFileID = |
| 84 |
SourceMgr.getOrCreateFileID(BuiltInFile, SrcMgr::C_User); |
| 85 |
SourceMgr.setMainFileID(BuiltInFileID); |
| 86 |
SourceLocation LocBuiltIn = |
| 87 |
SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); |
| 88 |
bool isWrittenInBuiltInFileTrue = |
| 89 |
SourceMgr.isWrittenInBuiltinFile(LocBuiltIn); |
| 90 |
|
| 91 |
std::unique_ptr<llvm::MemoryBuffer> CommandLineBuf = |
| 92 |
llvm::MemoryBuffer::getMemBuffer(Source); |
| 93 |
FileEntryRef CommandLineFile = FileMgr.getVirtualFileRef( |
| 94 |
"<command line>", CommandLineBuf->getBufferSize(), 0); |
| 95 |
SourceMgr.overrideFileContents(CommandLineFile, std::move(CommandLineBuf)); |
| 96 |
FileID CommandLineFileID = |
| 97 |
SourceMgr.getOrCreateFileID(CommandLineFile, SrcMgr::C_User); |
| 98 |
SourceMgr.setMainFileID(CommandLineFileID); |
| 99 |
SourceLocation LocCommandLine = |
| 100 |
SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); |
| 101 |
bool isWrittenInCommandLineFileTrue = |
| 102 |
SourceMgr.isWrittenInCommandLineFile(LocCommandLine); |
| 103 |
|
| 104 |
std::unique_ptr<llvm::MemoryBuffer> ScratchSpaceBuf = |
| 105 |
llvm::MemoryBuffer::getMemBuffer(Source); |
| 106 |
FileEntryRef ScratchSpaceFile = FileMgr.getVirtualFileRef( |
| 107 |
"<scratch space>", ScratchSpaceBuf->getBufferSize(), 0); |
| 108 |
SourceMgr.overrideFileContents(ScratchSpaceFile, std::move(ScratchSpaceBuf)); |
| 109 |
FileID ScratchSpaceFileID = |
| 110 |
SourceMgr.getOrCreateFileID(ScratchSpaceFile, SrcMgr::C_User); |
| 111 |
SourceMgr.setMainFileID(ScratchSpaceFileID); |
| 112 |
SourceLocation LocScratchSpace = |
| 113 |
SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); |
| 114 |
bool isWrittenInScratchSpaceTrue = |
| 115 |
SourceMgr.isWrittenInScratchSpace(LocScratchSpace); |
| 116 |
|
| 117 |
EXPECT_TRUE(isWrittenInBuiltInFileTrue); |
| 118 |
EXPECT_TRUE(isWrittenInCommandLineFileTrue); |
| 119 |
EXPECT_TRUE(isWrittenInScratchSpaceTrue); |
| 120 |
} |
| 121 |
|
| 122 |
TEST_F(SourceManagerTest, isInSystemHeader) { |
| 123 |
// Check for invalid source location |
| 124 |
SourceLocation LocEmpty; |
| 125 |
bool isInSystemHeaderFalse = SourceMgr.isInSystemHeader(LocEmpty); |
| 126 |
ASSERT_FALSE(isInSystemHeaderFalse); |
| 127 |
} |
| 128 |
|
| 129 |
TEST_F(SourceManagerTest, isBeforeInTranslationUnit) { |
| 130 |
const char *source = |
| 131 |
"#define M(x) [x]\n" |
| 132 |
"M(foo)"; |
| 133 |
std::unique_ptr<llvm::MemoryBuffer> Buf = |
| 134 |
llvm::MemoryBuffer::getMemBuffer(source); |
| 135 |
FileID mainFileID = SourceMgr.createFileID(std::move(Buf)); |
| 136 |
SourceMgr.setMainFileID(mainFileID); |
| 137 |
|
| 138 |
HeaderSearchOptions HSOpts; |
| 139 |
PreprocessorOptions PPOpts; |
| 140 |
TrivialModuleLoader ModLoader; |
| 141 |
HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, &*Target); |
| 142 |
Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, |
| 143 |
/*IILookup =*/nullptr, /*OwnsHeaderSearch =*/false); |
| 144 |
PP.Initialize(*Target); |
| 145 |
PP.EnterMainSourceFile(); |
| 146 |
|
| 147 |
std::vector<Token> toks; |
| 148 |
PP.LexTokensUntilEOF(&toks); |
| 149 |
|
| 150 |
// Make sure we got the tokens that we expected. |
| 151 |
ASSERT_EQ(3U, toks.size()); |
| 152 |
ASSERT_EQ(tok::l_square, toks[0].getKind()); |
| 153 |
ASSERT_EQ(tok::identifier, toks[1].getKind()); |
| 154 |
ASSERT_EQ(tok::r_square, toks[2].getKind()); |
| 155 |
|
| 156 |
SourceLocation lsqrLoc = toks[0].getLocation(); |
| 157 |
SourceLocation idLoc = toks[1].getLocation(); |
| 158 |
SourceLocation rsqrLoc = toks[2].getLocation(); |
| 159 |
|
| 160 |
SourceLocation macroExpStartLoc = SourceMgr.translateLineCol(mainFileID, 2, 1); |
| 161 |
SourceLocation macroExpEndLoc = SourceMgr.translateLineCol(mainFileID, 2, 6); |
| 162 |
ASSERT_TRUE(macroExpStartLoc.isFileID()); |
| 163 |
ASSERT_TRUE(macroExpEndLoc.isFileID()); |
| 164 |
|
| 165 |
SmallString<32> str; |
| 166 |
ASSERT_EQ("M", PP.getSpelling(macroExpStartLoc, str)); |
| 167 |
ASSERT_EQ(")", PP.getSpelling(macroExpEndLoc, str)); |
| 168 |
|
| 169 |
EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(lsqrLoc, idLoc)); |
| 170 |
EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, rsqrLoc)); |
| 171 |
EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(macroExpStartLoc, idLoc)); |
| 172 |
EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, macroExpEndLoc)); |
| 173 |
} |
| 174 |
|
| 175 |
TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithTokenSplit) { |
| 176 |
const char *main = R"cpp( |
| 177 |
#define ID(X) X |
| 178 |
ID( |
| 179 |
ID(a >> b) |
| 180 |
c |
| 181 |
) |
| 182 |
)cpp"; |
| 183 |
|
| 184 |
SourceMgr.setMainFileID( |
| 185 |
SourceMgr.createFileID(llvm::MemoryBuffer::getMemBuffer(main))); |
| 186 |
|
| 187 |
HeaderSearchOptions HSOpts; |
| 188 |
PreprocessorOptions PPOpts; |
| 189 |
TrivialModuleLoader ModLoader; |
| 190 |
HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, &*Target); |
| 191 |
Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, |
| 192 |
/*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); |
| 193 |
PP.Initialize(*Target); |
| 194 |
PP.EnterMainSourceFile(); |
| 195 |
llvm::SmallString<8> Scratch; |
| 196 |
|
| 197 |
std::vector<Token> toks; |
| 198 |
PP.LexTokensUntilEOF(&toks); |
| 199 |
|
| 200 |
// Make sure we got the tokens that we expected. |
| 201 |
ASSERT_EQ(4U, toks.size()) << "a >> b c"; |
| 202 |
// Sanity check their order. |
| 203 |
for (unsigned I = 0; I < toks.size() - 1; ++I) { |
| 204 |
EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(toks[I].getLocation(), |
| 205 |
toks[I + 1].getLocation())); |
| 206 |
EXPECT_FALSE(SourceMgr.isBeforeInTranslationUnit(toks[I + 1].getLocation(), |
| 207 |
toks[I].getLocation())); |
| 208 |
} |
| 209 |
|
| 210 |
// Split the >> into two > tokens, as happens when parsing nested templates. |
| 211 |
unsigned RightShiftIndex = 1; |
| 212 |
SourceLocation RightShift = toks[RightShiftIndex].getLocation(); |
| 213 |
EXPECT_EQ(">>", Lexer::getSpelling(SourceMgr.getSpellingLoc(RightShift), |
| 214 |
Scratch, SourceMgr, LangOpts)); |
| 215 |
SourceLocation Greater1 = PP.SplitToken(RightShift, /*Length=*/1); |
| 216 |
SourceLocation Greater2 = RightShift.getLocWithOffset(1); |
| 217 |
EXPECT_TRUE(Greater1.isMacroID()); |
| 218 |
EXPECT_EQ(">", Lexer::getSpelling(SourceMgr.getSpellingLoc(Greater1), Scratch, |
| 219 |
SourceMgr, LangOpts)); |
| 220 |
EXPECT_EQ(">", Lexer::getSpelling(SourceMgr.getSpellingLoc(Greater2), Scratch, |
| 221 |
SourceMgr, LangOpts)); |
| 222 |
EXPECT_EQ(SourceMgr.getImmediateExpansionRange(Greater1).getBegin(), |
| 223 |
RightShift); |
| 224 |
|
| 225 |
for (unsigned I = 0; I < toks.size(); ++I) { |
| 226 |
SCOPED_TRACE("Token " + std::to_string(I)); |
| 227 |
// Right-shift is the parent of Greater1, so it compares less. |
| 228 |
EXPECT_EQ( |
| 229 |
SourceMgr.isBeforeInTranslationUnit(toks[I].getLocation(), Greater1), |
| 230 |
I <= RightShiftIndex); |
| 231 |
EXPECT_EQ( |
| 232 |
SourceMgr.isBeforeInTranslationUnit(toks[I].getLocation(), Greater2), |
| 233 |
I <= RightShiftIndex); |
| 234 |
EXPECT_EQ( |
| 235 |
SourceMgr.isBeforeInTranslationUnit(Greater1, toks[I].getLocation()), |
| 236 |
RightShiftIndex < I); |
| 237 |
EXPECT_EQ( |
| 238 |
SourceMgr.isBeforeInTranslationUnit(Greater2, toks[I].getLocation()), |
| 239 |
RightShiftIndex < I); |
| 240 |
} |
| 241 |
EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Greater1, Greater2)); |
| 242 |
EXPECT_FALSE(SourceMgr.isBeforeInTranslationUnit(Greater2, Greater1)); |
| 243 |
} |
| 244 |
|
| 245 |
TEST_F(SourceManagerTest, getColumnNumber) { |
| 246 |
const char *Source = |
| 247 |
"int x;\n" |
| 248 |
"int y;"; |
| 249 |
|
| 250 |
std::unique_ptr<llvm::MemoryBuffer> Buf = |
| 251 |
llvm::MemoryBuffer::getMemBuffer(Source); |
| 252 |
FileID MainFileID = SourceMgr.createFileID(std::move(Buf)); |
| 253 |
SourceMgr.setMainFileID(MainFileID); |
| 254 |
|
| 255 |
bool Invalid; |
| 256 |
|
| 257 |
Invalid = false; |
| 258 |
EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, &Invalid)); |
| 259 |
EXPECT_TRUE(!Invalid); |
| 260 |
|
| 261 |
Invalid = false; |
| 262 |
EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 4, &Invalid)); |
| 263 |
EXPECT_TRUE(!Invalid); |
| 264 |
|
| 265 |
Invalid = false; |
| 266 |
EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 7, &Invalid)); |
| 267 |
EXPECT_TRUE(!Invalid); |
| 268 |
|
| 269 |
Invalid = false; |
| 270 |
EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 11, &Invalid)); |
| 271 |
EXPECT_TRUE(!Invalid); |
| 272 |
|
| 273 |
Invalid = false; |
| 274 |
EXPECT_EQ(7U, SourceMgr.getColumnNumber(MainFileID, strlen(Source), |
| 275 |
&Invalid)); |
| 276 |
EXPECT_TRUE(!Invalid); |
| 277 |
|
| 278 |
Invalid = false; |
| 279 |
SourceMgr.getColumnNumber(MainFileID, strlen(Source)+1, &Invalid); |
| 280 |
EXPECT_TRUE(Invalid); |
| 281 |
|
| 282 |
// Test invalid files |
| 283 |
Invalid = false; |
| 284 |
SourceMgr.getColumnNumber(FileID(), 0, &Invalid); |
| 285 |
EXPECT_TRUE(Invalid); |
| 286 |
|
| 287 |
Invalid = false; |
| 288 |
SourceMgr.getColumnNumber(FileID(), 1, &Invalid); |
| 289 |
EXPECT_TRUE(Invalid); |
| 290 |
|
| 291 |
// Test with no invalid flag. |
| 292 |
EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, nullptr)); |
| 293 |
} |
| 294 |
|
| 295 |
TEST_F(SourceManagerTest, locationPrintTest) { |
| 296 |
const char *header = "#define IDENTITY(x) x\n"; |
| 297 |
|
| 298 |
const char *Source = "int x;\n" |
| 299 |
"include \"test-header.h\"\n" |
| 300 |
"IDENTITY(int y);\n" |
| 301 |
"int z;"; |
| 302 |
|
| 303 |
std::unique_ptr<llvm::MemoryBuffer> HeaderBuf = |
| 304 |
llvm::MemoryBuffer::getMemBuffer(header); |
| 305 |
std::unique_ptr<llvm::MemoryBuffer> Buf = |
| 306 |
llvm::MemoryBuffer::getMemBuffer(Source); |
| 307 |
|
| 308 |
FileEntryRef SourceFile = |
| 309 |
FileMgr.getVirtualFileRef("/mainFile.cpp", Buf->getBufferSize(), 0); |
| 310 |
SourceMgr.overrideFileContents(SourceFile, std::move(Buf)); |
| 311 |
|
| 312 |
FileEntryRef HeaderFile = FileMgr.getVirtualFileRef( |
| 313 |
"/test-header.h", HeaderBuf->getBufferSize(), 0); |
| 314 |
SourceMgr.overrideFileContents(HeaderFile, std::move(HeaderBuf)); |
| 315 |
|
| 316 |
FileID MainFileID = SourceMgr.getOrCreateFileID(SourceFile, SrcMgr::C_User); |
| 317 |
FileID HeaderFileID = SourceMgr.getOrCreateFileID(HeaderFile, SrcMgr::C_User); |
| 318 |
SourceMgr.setMainFileID(MainFileID); |
| 319 |
|
| 320 |
auto BeginLoc = SourceMgr.getLocForStartOfFile(MainFileID); |
| 321 |
auto EndLoc = SourceMgr.getLocForEndOfFile(MainFileID); |
| 322 |
|
| 323 |
auto BeginEOLLoc = SourceMgr.translateLineCol(MainFileID, 1, 7); |
| 324 |
|
| 325 |
auto HeaderLoc = SourceMgr.getLocForStartOfFile(HeaderFileID); |
| 326 |
|
| 327 |
EXPECT_EQ(BeginLoc.printToString(SourceMgr), "/mainFile.cpp:1:1"); |
| 328 |
EXPECT_EQ(EndLoc.printToString(SourceMgr), "/mainFile.cpp:4:7"); |
| 329 |
|
| 330 |
EXPECT_EQ(BeginEOLLoc.printToString(SourceMgr), "/mainFile.cpp:1:7"); |
| 331 |
EXPECT_EQ(HeaderLoc.printToString(SourceMgr), "/test-header.h:1:1"); |
| 332 |
|
| 333 |
EXPECT_EQ(SourceRange(BeginLoc, BeginLoc).printToString(SourceMgr), |
| 334 |
"</mainFile.cpp:1:1>"); |
| 335 |
EXPECT_EQ(SourceRange(BeginLoc, BeginEOLLoc).printToString(SourceMgr), |
| 336 |
"</mainFile.cpp:1:1, col:7>"); |
| 337 |
EXPECT_EQ(SourceRange(BeginLoc, EndLoc).printToString(SourceMgr), |
| 338 |
"</mainFile.cpp:1:1, line:4:7>"); |
| 339 |
EXPECT_EQ(SourceRange(BeginLoc, HeaderLoc).printToString(SourceMgr), |
| 340 |
"</mainFile.cpp:1:1, /test-header.h:1:1>"); |
| 341 |
} |
| 342 |
|
| 343 |
TEST_F(SourceManagerTest, getInvalidBOM) { |
| 344 |
ASSERT_EQ(SrcMgr::ContentCache::getInvalidBOM(""), nullptr); |
| 345 |
ASSERT_EQ(SrcMgr::ContentCache::getInvalidBOM("\x00\x00\x00"), nullptr); |
| 346 |
ASSERT_EQ(SrcMgr::ContentCache::getInvalidBOM("\xFF\xFF\xFF"), nullptr); |
| 347 |
ASSERT_EQ(SrcMgr::ContentCache::getInvalidBOM("#include <iostream>"), |
| 348 |
nullptr); |
| 349 |
|
| 350 |
ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM( |
| 351 |
"\xFE\xFF#include <iostream>")), |
| 352 |
"UTF-16 (BE)"); |
| 353 |
ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM( |
| 354 |
"\xFF\xFE#include <iostream>")), |
| 355 |
"UTF-16 (LE)"); |
| 356 |
ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM( |
| 357 |
"\x2B\x2F\x76#include <iostream>")), |
| 358 |
"UTF-7"); |
| 359 |
ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM( |
| 360 |
"\xF7\x64\x4C#include <iostream>")), |
| 361 |
"UTF-1"); |
| 362 |
ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM( |
| 363 |
"\xDD\x73\x66\x73#include <iostream>")), |
| 364 |
"UTF-EBCDIC"); |
| 365 |
ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM( |
| 366 |
"\x0E\xFE\xFF#include <iostream>")), |
| 367 |
"SCSU"); |
| 368 |
ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM( |
| 369 |
"\xFB\xEE\x28#include <iostream>")), |
| 370 |
"BOCU-1"); |
| 371 |
ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM( |
| 372 |
"\x84\x31\x95\x33#include <iostream>")), |
| 373 |
"GB-18030"); |
| 374 |
ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM( |
| 375 |
llvm::StringLiteral::withInnerNUL( |
| 376 |
"\x00\x00\xFE\xFF#include <iostream>"))), |
| 377 |
"UTF-32 (BE)"); |
| 378 |
ASSERT_EQ(StringRef(SrcMgr::ContentCache::getInvalidBOM( |
| 379 |
llvm::StringLiteral::withInnerNUL( |
| 380 |
"\xFF\xFE\x00\x00#include <iostream>"))), |
| 381 |
"UTF-32 (LE)"); |
| 382 |
} |
| 383 |
|
| 384 |
TEST_F(SourceManagerTest, sourceRangeWorksWithDenseSet) { |
| 385 |
llvm::DenseSet<SourceRange> Set; |
| 386 |
SourceRange TestRange = {SourceLocation::getFromRawEncoding(10), |
| 387 |
SourceLocation::getFromRawEncoding(11)}; |
| 388 |
ASSERT_THAT(Set, IsEmpty()); |
| 389 |
Set.insert(TestRange); |
| 390 |
ASSERT_EQ(Set.size(), 1U); |
| 391 |
ASSERT_TRUE(Set.contains(TestRange)); |
| 392 |
ASSERT_FALSE(Set.contains({SourceLocation::getFromRawEncoding(10), |
| 393 |
SourceLocation::getFromRawEncoding(10)})); |
| 394 |
Set.erase(TestRange); |
| 395 |
ASSERT_THAT(Set, IsEmpty()); |
| 396 |
} |
| 397 |
|
| 398 |
// Regression test - there was an out of bound access for buffers not terminated by zero. |
| 399 |
TEST_F(SourceManagerTest, getLineNumber) { |
| 400 |
const unsigned pageSize = llvm::sys::Process::getPageSizeEstimate(); |
| 401 |
std::unique_ptr<char[]> source(new char[pageSize]); |
| 402 |
for(unsigned i = 0; i < pageSize; ++i) { |
| 403 |
source[i] = 'a'; |
| 404 |
} |
| 405 |
|
| 406 |
std::unique_ptr<llvm::MemoryBuffer> Buf = |
| 407 |
llvm::MemoryBuffer::getMemBuffer( |
| 408 |
llvm::MemoryBufferRef( |
| 409 |
llvm::StringRef(source.get(), 3), "whatever" |
| 410 |
), |
| 411 |
false |
| 412 |
); |
| 413 |
|
| 414 |
FileID mainFileID = SourceMgr.createFileID(std::move(Buf)); |
| 415 |
SourceMgr.setMainFileID(mainFileID); |
| 416 |
|
| 417 |
ASSERT_NO_FATAL_FAILURE(SourceMgr.getLineNumber(mainFileID, 1, nullptr)); |
| 418 |
} |
| 419 |
|
| 420 |
struct FakeExternalSLocEntrySource : ExternalSLocEntrySource { |
| 421 |
bool ReadSLocEntry(int ID) override { return {}; } |
| 422 |
int getSLocEntryID(SourceLocation::UIntTy SLocOffset) override { return 0; } |
| 423 |
std::pair<SourceLocation, StringRef> getModuleImportLoc(int ID) override { |
| 424 |
return {}; |
| 425 |
} |
| 426 |
}; |
| 427 |
|
| 428 |
TEST_F(SourceManagerTest, loadedSLocEntryIsInTheSameTranslationUnit) { |
| 429 |
auto InSameTU = [=](int LID, int RID) { |
| 430 |
return SourceMgr.isInTheSameTranslationUnitImpl( |
| 431 |
std::make_pair(SourceManagerTestHelper::makeFileID(LID), 0), |
| 432 |
std::make_pair(SourceManagerTestHelper::makeFileID(RID), 0)); |
| 433 |
}; |
| 434 |
|
| 435 |
FakeExternalSLocEntrySource ExternalSource; |
| 436 |
SourceMgr.setExternalSLocEntrySource(&ExternalSource); |
| 437 |
|
| 438 |
unsigned ANumFileIDs = 10; |
| 439 |
auto [AFirstID, X] = SourceMgr.AllocateLoadedSLocEntries(ANumFileIDs, 10); |
| 440 |
int ALastID = AFirstID + ANumFileIDs - 1; |
| 441 |
// FileID(-11)..FileID(-2) |
| 442 |
ASSERT_EQ(AFirstID, -11); |
| 443 |
ASSERT_EQ(ALastID, -2); |
| 444 |
|
| 445 |
unsigned BNumFileIDs = 20; |
| 446 |
auto [BFirstID, Y] = SourceMgr.AllocateLoadedSLocEntries(BNumFileIDs, 20); |
| 447 |
int BLastID = BFirstID + BNumFileIDs - 1; |
| 448 |
// FileID(-31)..FileID(-12) |
| 449 |
ASSERT_EQ(BFirstID, -31); |
| 450 |
ASSERT_EQ(BLastID, -12); |
| 451 |
|
| 452 |
// Loaded vs local. |
| 453 |
EXPECT_FALSE(InSameTU(-2, 1)); |
| 454 |
|
| 455 |
// Loaded in the same allocation A. |
| 456 |
EXPECT_TRUE(InSameTU(-11, -2)); |
| 457 |
EXPECT_TRUE(InSameTU(-11, -6)); |
| 458 |
|
| 459 |
// Loaded in the same allocation B. |
| 460 |
EXPECT_TRUE(InSameTU(-31, -12)); |
| 461 |
EXPECT_TRUE(InSameTU(-31, -16)); |
| 462 |
|
| 463 |
// Loaded from different allocations A and B. |
| 464 |
EXPECT_FALSE(InSameTU(-12, -11)); |
| 465 |
} |
| 466 |
|
| 467 |
#if defined(LLVM_ON_UNIX) |
| 468 |
|
| 469 |
// A single SourceManager instance is sometimes reused across multiple |
| 470 |
// compilations. This test makes sure we're resetting caches built for tracking |
| 471 |
// include locations that are based on FileIDs, to make sure we don't report |
| 472 |
// wrong include locations when FileIDs coincide between two different runs. |
| 473 |
TEST_F(SourceManagerTest, ResetsIncludeLocMap) { |
| 474 |
auto ParseFile = [&] { |
| 475 |
TrivialModuleLoader ModLoader; |
| 476 |
HeaderSearchOptions HSOpts; |
| 477 |
PreprocessorOptions PPOpts; |
| 478 |
HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, &*Target); |
| 479 |
Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, |
| 480 |
/*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); |
| 481 |
PP.Initialize(*Target); |
| 482 |
PP.EnterMainSourceFile(); |
| 483 |
PP.LexTokensUntilEOF(); |
| 484 |
EXPECT_FALSE(Diags.hasErrorOccurred()); |
| 485 |
}; |
| 486 |
|
| 487 |
auto Buf = llvm::MemoryBuffer::getMemBuffer(""); |
| 488 |
FileEntryRef HeaderFile = |
| 489 |
FileMgr.getVirtualFileRef("/foo.h", Buf->getBufferSize(), 0); |
| 490 |
SourceMgr.overrideFileContents(HeaderFile, std::move(Buf)); |
| 491 |
|
| 492 |
Buf = llvm::MemoryBuffer::getMemBuffer(R"cpp(#include "/foo.h")cpp"); |
| 493 |
FileEntryRef BarFile = |
| 494 |
FileMgr.getVirtualFileRef("/bar.h", Buf->getBufferSize(), 0); |
| 495 |
SourceMgr.overrideFileContents(BarFile, std::move(Buf)); |
| 496 |
SourceMgr.createFileID(BarFile, {}, clang::SrcMgr::C_User); |
| 497 |
|
| 498 |
Buf = llvm::MemoryBuffer::getMemBuffer(R"cpp(#include "/foo.h")cpp"); |
| 499 |
FileID MFID = SourceMgr.createFileID(std::move(Buf)); |
| 500 |
SourceMgr.setMainFileID(MFID); |
| 501 |
|
| 502 |
ParseFile(); |
| 503 |
auto FooFID = SourceMgr.getOrCreateFileID(HeaderFile, clang::SrcMgr::C_User); |
| 504 |
auto IncFID = SourceMgr.getDecomposedIncludedLoc(FooFID).first; |
| 505 |
EXPECT_EQ(IncFID, MFID); |
| 506 |
|
| 507 |
// Clean up source-manager state before we start next parse. |
| 508 |
SourceMgr.clearIDTables(); |
| 509 |
|
| 510 |
// Set up a new main file. |
| 511 |
Buf = llvm::MemoryBuffer::getMemBuffer(R"cpp( |
| 512 |
// silly comment 42 |
| 513 |
#include "/bar.h")cpp"); |
| 514 |
MFID = SourceMgr.createFileID(std::move(Buf)); |
| 515 |
SourceMgr.setMainFileID(MFID); |
| 516 |
|
| 517 |
ParseFile(); |
| 518 |
// Make sure foo.h got the same file-id in both runs. |
| 519 |
EXPECT_EQ(FooFID, |
| 520 |
SourceMgr.getOrCreateFileID(HeaderFile, clang::SrcMgr::C_User)); |
| 521 |
auto BarFID = SourceMgr.getOrCreateFileID(BarFile, clang::SrcMgr::C_User); |
| 522 |
IncFID = SourceMgr.getDecomposedIncludedLoc(FooFID).first; |
| 523 |
// Check that includer is bar.h during this run. |
| 524 |
EXPECT_EQ(IncFID, BarFID); |
| 525 |
} |
| 526 |
|
| 527 |
TEST_F(SourceManagerTest, getMacroArgExpandedLocation) { |
| 528 |
const char *header = |
| 529 |
"#define FM(x,y) x\n"; |
| 530 |
|
| 531 |
const char *main = |
| 532 |
"#include \"/test-header.h\"\n" |
| 533 |
"#define VAL 0\n" |
| 534 |
"FM(VAL,0)\n" |
| 535 |
"FM(0,VAL)\n" |
| 536 |
"FM(FM(0,VAL),0)\n" |
| 537 |
"#define CONCAT(X, Y) X##Y\n" |
| 538 |
"CONCAT(1,1)\n"; |
| 539 |
|
| 540 |
std::unique_ptr<llvm::MemoryBuffer> HeaderBuf = |
| 541 |
llvm::MemoryBuffer::getMemBuffer(header); |
| 542 |
std::unique_ptr<llvm::MemoryBuffer> MainBuf = |
| 543 |
llvm::MemoryBuffer::getMemBuffer(main); |
| 544 |
FileID mainFileID = SourceMgr.createFileID(std::move(MainBuf)); |
| 545 |
SourceMgr.setMainFileID(mainFileID); |
| 546 |
|
| 547 |
FileEntryRef headerFile = FileMgr.getVirtualFileRef( |
| 548 |
"/test-header.h", HeaderBuf->getBufferSize(), 0); |
| 549 |
SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf)); |
| 550 |
|
| 551 |
HeaderSearchOptions HSOpts; |
| 552 |
PreprocessorOptions PPOpts; |
| 553 |
TrivialModuleLoader ModLoader; |
| 554 |
HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, &*Target); |
| 555 |
|
| 556 |
Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, |
| 557 |
/*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); |
| 558 |
// Ensure we can get expanded locations in presence of implicit includes. |
| 559 |
// These are different than normal includes since predefines buffer doesn't |
| 560 |
// have a valid insertion location. |
| 561 |
PP.setPredefines("#include \"/implicit-header.h\""); |
| 562 |
FileMgr.getVirtualFileRef("/implicit-header.h", 0, 0); |
| 563 |
PP.Initialize(*Target); |
| 564 |
PP.EnterMainSourceFile(); |
| 565 |
|
| 566 |
std::vector<Token> toks; |
| 567 |
PP.LexTokensUntilEOF(&toks); |
| 568 |
|
| 569 |
// Make sure we got the tokens that we expected. |
| 570 |
ASSERT_EQ(4U, toks.size()); |
| 571 |
ASSERT_EQ(tok::numeric_constant, toks[0].getKind()); |
| 572 |
ASSERT_EQ(tok::numeric_constant, toks[1].getKind()); |
| 573 |
ASSERT_EQ(tok::numeric_constant, toks[2].getKind()); |
| 574 |
ASSERT_EQ(tok::numeric_constant, toks[3].getKind()); |
| 575 |
|
| 576 |
SourceLocation defLoc = SourceMgr.translateLineCol(mainFileID, 2, 13); |
| 577 |
SourceLocation loc1 = SourceMgr.translateLineCol(mainFileID, 3, 8); |
| 578 |
SourceLocation loc2 = SourceMgr.translateLineCol(mainFileID, 4, 4); |
| 579 |
SourceLocation loc3 = SourceMgr.translateLineCol(mainFileID, 5, 7); |
| 580 |
SourceLocation defLoc2 = SourceMgr.translateLineCol(mainFileID, 6, 22); |
| 581 |
defLoc = SourceMgr.getMacroArgExpandedLocation(defLoc); |
| 582 |
loc1 = SourceMgr.getMacroArgExpandedLocation(loc1); |
| 583 |
loc2 = SourceMgr.getMacroArgExpandedLocation(loc2); |
| 584 |
loc3 = SourceMgr.getMacroArgExpandedLocation(loc3); |
| 585 |
defLoc2 = SourceMgr.getMacroArgExpandedLocation(defLoc2); |
| 586 |
|
| 587 |
EXPECT_TRUE(defLoc.isFileID()); |
| 588 |
EXPECT_TRUE(loc1.isFileID()); |
| 589 |
EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc2)); |
| 590 |
EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc3)); |
| 591 |
EXPECT_EQ(loc2, toks[1].getLocation()); |
| 592 |
EXPECT_EQ(loc3, toks[2].getLocation()); |
| 593 |
EXPECT_TRUE(defLoc2.isFileID()); |
| 594 |
} |
| 595 |
|
| 596 |
namespace { |
| 597 |
|
| 598 |
struct MacroAction { |
| 599 |
enum Kind { kExpansion, kDefinition, kUnDefinition}; |
| 600 |
|
| 601 |
SourceLocation Loc; |
| 602 |
std::string Name; |
| 603 |
LLVM_PREFERRED_TYPE(Kind) |
| 604 |
unsigned MAKind : 3; |
| 605 |
|
| 606 |
MacroAction(SourceLocation Loc, StringRef Name, unsigned K) |
| 607 |
: Loc(Loc), Name(std::string(Name)), MAKind(K) {} |
| 608 |
|
| 609 |
bool isExpansion() const { return MAKind == kExpansion; } |
| 610 |
bool isDefinition() const { return MAKind & kDefinition; } |
| 611 |
bool isUnDefinition() const { return MAKind & kUnDefinition; } |
| 612 |
}; |
| 613 |
|
| 614 |
class MacroTracker : public PPCallbacks { |
| 615 |
std::vector<MacroAction> &Macros; |
| 616 |
|
| 617 |
public: |
| 618 |
explicit MacroTracker(std::vector<MacroAction> &Macros) : Macros(Macros) { } |
| 619 |
|
| 620 |
void MacroDefined(const Token &MacroNameTok, |
| 621 |
const MacroDirective *MD) override { |
| 622 |
Macros.push_back(MacroAction(MD->getLocation(), |
| 623 |
MacroNameTok.getIdentifierInfo()->getName(), |
| 624 |
MacroAction::kDefinition)); |
| 625 |
} |
| 626 |
void MacroUndefined(const Token &MacroNameTok, |
| 627 |
const MacroDefinition &MD, |
| 628 |
const MacroDirective *UD) override { |
| 629 |
Macros.push_back( |
| 630 |
MacroAction(UD ? UD->getLocation() : SourceLocation(), |
| 631 |
MacroNameTok.getIdentifierInfo()->getName(), |
| 632 |
UD ? MacroAction::kDefinition | MacroAction::kUnDefinition |
| 633 |
: MacroAction::kUnDefinition)); |
| 634 |
} |
| 635 |
void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD, |
| 636 |
SourceRange Range, const MacroArgs *Args) override { |
| 637 |
Macros.push_back(MacroAction(MacroNameTok.getLocation(), |
| 638 |
MacroNameTok.getIdentifierInfo()->getName(), |
| 639 |
MacroAction::kExpansion)); |
| 640 |
} |
| 641 |
}; |
| 642 |
|
| 643 |
} |
| 644 |
|
| 645 |
TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) { |
| 646 |
const char *header = |
| 647 |
"#define MACRO_IN_INCLUDE 0\n" |
| 648 |
"#define MACRO_DEFINED\n" |
| 649 |
"#undef MACRO_DEFINED\n" |
| 650 |
"#undef MACRO_UNDEFINED\n"; |
| 651 |
|
| 652 |
const char *main = |
| 653 |
"#define M(x) x\n" |
| 654 |
"#define INC \"/test-header.h\"\n" |
| 655 |
"#include M(INC)\n" |
| 656 |
"#define INC2 </test-header.h>\n" |
| 657 |
"#include M(INC2)\n"; |
| 658 |
|
| 659 |
std::unique_ptr<llvm::MemoryBuffer> HeaderBuf = |
| 660 |
llvm::MemoryBuffer::getMemBuffer(header); |
| 661 |
std::unique_ptr<llvm::MemoryBuffer> MainBuf = |
| 662 |
llvm::MemoryBuffer::getMemBuffer(main); |
| 663 |
SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(MainBuf))); |
| 664 |
|
| 665 |
FileEntryRef headerFile = FileMgr.getVirtualFileRef( |
| 666 |
"/test-header.h", HeaderBuf->getBufferSize(), 0); |
| 667 |
SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf)); |
| 668 |
|
| 669 |
HeaderSearchOptions HSOpts; |
| 670 |
PreprocessorOptions PPOpts; |
| 671 |
TrivialModuleLoader ModLoader; |
| 672 |
HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, &*Target); |
| 673 |
Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, |
| 674 |
/*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); |
| 675 |
PP.Initialize(*Target); |
| 676 |
|
| 677 |
std::vector<MacroAction> Macros; |
| 678 |
PP.addPPCallbacks(std::make_unique<MacroTracker>(Macros)); |
| 679 |
|
| 680 |
PP.EnterMainSourceFile(); |
| 681 |
|
| 682 |
std::vector<Token> toks; |
| 683 |
PP.LexTokensUntilEOF(&toks); |
| 684 |
|
| 685 |
// Make sure we got the tokens that we expected. |
| 686 |
ASSERT_EQ(0U, toks.size()); |
| 687 |
|
| 688 |
ASSERT_EQ(15U, Macros.size()); |
| 689 |
// #define M(x) x |
| 690 |
ASSERT_TRUE(Macros[0].isDefinition()); |
| 691 |
ASSERT_EQ("M", Macros[0].Name); |
| 692 |
// #define INC "/test-header.h" |
| 693 |
ASSERT_TRUE(Macros[1].isDefinition()); |
| 694 |
ASSERT_EQ("INC", Macros[1].Name); |
| 695 |
// M expansion in #include M(INC) |
| 696 |
ASSERT_FALSE(Macros[2].isDefinition()); |
| 697 |
ASSERT_EQ("M", Macros[2].Name); |
| 698 |
// INC expansion in #include M(INC) |
| 699 |
ASSERT_TRUE(Macros[3].isExpansion()); |
| 700 |
ASSERT_EQ("INC", Macros[3].Name); |
| 701 |
// #define MACRO_IN_INCLUDE 0 |
| 702 |
ASSERT_TRUE(Macros[4].isDefinition()); |
| 703 |
ASSERT_EQ("MACRO_IN_INCLUDE", Macros[4].Name); |
| 704 |
// #define MACRO_DEFINED |
| 705 |
ASSERT_TRUE(Macros[5].isDefinition()); |
| 706 |
ASSERT_FALSE(Macros[5].isUnDefinition()); |
| 707 |
ASSERT_EQ("MACRO_DEFINED", Macros[5].Name); |
| 708 |
// #undef MACRO_DEFINED |
| 709 |
ASSERT_TRUE(Macros[6].isDefinition()); |
| 710 |
ASSERT_TRUE(Macros[6].isUnDefinition()); |
| 711 |
ASSERT_EQ("MACRO_DEFINED", Macros[6].Name); |
| 712 |
// #undef MACRO_UNDEFINED |
| 713 |
ASSERT_FALSE(Macros[7].isDefinition()); |
| 714 |
ASSERT_TRUE(Macros[7].isUnDefinition()); |
| 715 |
ASSERT_EQ("MACRO_UNDEFINED", Macros[7].Name); |
| 716 |
// #define INC2 </test-header.h> |
| 717 |
ASSERT_TRUE(Macros[8].isDefinition()); |
| 718 |
ASSERT_EQ("INC2", Macros[8].Name); |
| 719 |
// M expansion in #include M(INC2) |
| 720 |
ASSERT_FALSE(Macros[9].isDefinition()); |
| 721 |
ASSERT_EQ("M", Macros[9].Name); |
| 722 |
// INC2 expansion in #include M(INC2) |
| 723 |
ASSERT_TRUE(Macros[10].isExpansion()); |
| 724 |
ASSERT_EQ("INC2", Macros[10].Name); |
| 725 |
// #define MACRO_IN_INCLUDE 0 |
| 726 |
ASSERT_TRUE(Macros[11].isDefinition()); |
| 727 |
ASSERT_EQ("MACRO_IN_INCLUDE", Macros[11].Name); |
| 728 |
|
| 729 |
// The INC expansion in #include M(INC) comes before the first |
| 730 |
// MACRO_IN_INCLUDE definition of the included file. |
| 731 |
EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[3].Loc, Macros[4].Loc)); |
| 732 |
|
| 733 |
// The INC2 expansion in #include M(INC2) comes before the second |
| 734 |
// MACRO_IN_INCLUDE definition of the included file. |
| 735 |
EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[10].Loc, Macros[11].Loc)); |
| 736 |
} |
| 737 |
|
| 738 |
TEST_F(SourceManagerTest, isMainFile) { |
| 739 |
const char *Source = "int x;"; |
| 740 |
|
| 741 |
std::unique_ptr<llvm::MemoryBuffer> Buf = |
| 742 |
llvm::MemoryBuffer::getMemBuffer(Source); |
| 743 |
FileEntryRef SourceFile = |
| 744 |
FileMgr.getVirtualFileRef("mainFile.cpp", Buf->getBufferSize(), 0); |
| 745 |
SourceMgr.overrideFileContents(SourceFile, std::move(Buf)); |
| 746 |
|
| 747 |
std::unique_ptr<llvm::MemoryBuffer> Buf2 = |
| 748 |
llvm::MemoryBuffer::getMemBuffer(Source); |
| 749 |
FileEntryRef SecondFile = |
| 750 |
FileMgr.getVirtualFileRef("file2.cpp", Buf2->getBufferSize(), 0); |
| 751 |
SourceMgr.overrideFileContents(SecondFile, std::move(Buf2)); |
| 752 |
|
| 753 |
FileID MainFileID = SourceMgr.getOrCreateFileID(SourceFile, SrcMgr::C_User); |
| 754 |
SourceMgr.setMainFileID(MainFileID); |
| 755 |
|
| 756 |
EXPECT_TRUE(SourceMgr.isMainFile(*SourceFile)); |
| 757 |
EXPECT_TRUE(SourceMgr.isMainFile(*SourceFile)); |
| 758 |
EXPECT_FALSE(SourceMgr.isMainFile(*SecondFile)); |
| 759 |
} |
| 760 |
|
| 761 |
#endif |
| 762 |
|
| 763 |
} // anonymous namespace |