Imported Upstream version 6.10.0.49

Former-commit-id: 1d6753294b2993e1fbf92de9366bb9544db4189b
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2020-01-16 16:38:04 +00:00
parent d94e79959b
commit 468663ddbb
48518 changed files with 2789335 additions and 61176 deletions

View File

@ -0,0 +1,87 @@
//===- unittests/Frontend/ASTUnitTest.cpp - ASTUnit tests -----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <fstream>
#include "clang/Frontend/ASTUnit.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/CompilerInvocation.h"
#include "clang/Frontend/PCHContainerOperations.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/ToolOutputFile.h"
#include "gtest/gtest.h"
using namespace llvm;
using namespace clang;
namespace {
TEST(ASTUnit, SaveLoadPreservesLangOptionsInPrintingPolicy) {
// Check that the printing policy is restored with the correct language
// options when loading an ASTUnit from a file. To this end, an ASTUnit
// for a C++ translation unit is set up and written to a temporary file.
// By default `UseVoidForZeroParams` is true for non-C++ language options,
// thus we can check this field after loading the ASTUnit to deduce whether
// the correct (C++) language options were used when setting up the printing
// policy.
{
PrintingPolicy PolicyWithDefaultLangOpt(LangOptions{});
EXPECT_TRUE(PolicyWithDefaultLangOpt.UseVoidForZeroParams);
}
int FD;
llvm::SmallString<256> InputFileName;
ASSERT_FALSE(llvm::sys::fs::createTemporaryFile("ast-unit", "cpp", FD, InputFileName));
ToolOutputFile input_file(InputFileName, FD);
input_file.os() << "";
const char* Args[] = {"clang", "-xc++", InputFileName.c_str()};
IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
CompilerInstance::createDiagnostics(new DiagnosticOptions());
std::shared_ptr<CompilerInvocation> CInvok =
createInvocationFromCommandLine(Args, Diags);
if (!CInvok)
FAIL() << "could not create compiler invocation";
FileManager *FileMgr =
new FileManager(FileSystemOptions(), vfs::getRealFileSystem());
auto PCHContainerOps = std::make_shared<PCHContainerOperations>();
std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCompilerInvocation(
CInvok, PCHContainerOps, Diags, FileMgr);
if (!AST)
FAIL() << "failed to create ASTUnit";
EXPECT_FALSE(AST->getASTContext().getPrintingPolicy().UseVoidForZeroParams);
llvm::SmallString<256> ASTFileName;
ASSERT_FALSE(llvm::sys::fs::createTemporaryFile("ast-unit", "ast", FD, ASTFileName));
ToolOutputFile ast_file(ASTFileName, FD);
AST->Save(ASTFileName.str());
EXPECT_TRUE(llvm::sys::fs::exists(ASTFileName));
std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
ASTFileName.str(), PCHContainerOps->getRawReader(), ASTUnit::LoadEverything, Diags,
FileSystemOptions(), /*UseDebugInfo=*/false);
if (!AU)
FAIL() << "failed to load ASTUnit";
EXPECT_FALSE(AU->getASTContext().getPrintingPolicy().UseVoidForZeroParams);
}
} // anonymous namespace

View File

@ -0,0 +1,21 @@
set(LLVM_LINK_COMPONENTS
Support
)
add_clang_unittest(FrontendTests
ASTUnitTest.cpp
CompilerInstanceTest.cpp
FrontendActionTest.cpp
CodeGenActionTest.cpp
ParsedSourceLocationTest.cpp
PCHPreambleTest.cpp
)
target_link_libraries(FrontendTests
PRIVATE
clangAST
clangBasic
clangFrontend
clangLex
clangSema
clangCodeGen
)

View File

@ -0,0 +1,62 @@
//===- unittests/Frontend/CodeGenActionTest.cpp --- FrontendAction tests --===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Unit tests for CodeGenAction.
//
//===----------------------------------------------------------------------===//
#include "clang/CodeGen/BackendUtil.h"
#include "clang/CodeGen/CodeGenAction.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/PreprocessorOptions.h"
#include "gtest/gtest.h"
using namespace llvm;
using namespace clang;
using namespace clang::frontend;
namespace {
class NullCodeGenAction : public CodeGenAction {
public:
NullCodeGenAction(llvm::LLVMContext *_VMContext = nullptr)
: CodeGenAction(Backend_EmitMCNull, _VMContext) {}
// The action does not call methods of ATContext.
void ExecuteAction() override {
CompilerInstance &CI = getCompilerInstance();
if (!CI.hasPreprocessor())
return;
if (!CI.hasSema())
CI.createSema(getTranslationUnitKind(), nullptr);
}
};
TEST(CodeGenTest, TestNullCodeGen) {
auto Invocation = std::make_shared<CompilerInvocation>();
Invocation->getPreprocessorOpts().addRemappedFile(
"test.cc",
MemoryBuffer::getMemBuffer("").release());
Invocation->getFrontendOpts().Inputs.push_back(
FrontendInputFile("test.cc", InputKind::CXX));
Invocation->getFrontendOpts().ProgramAction = EmitLLVM;
Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
CompilerInstance Compiler;
Compiler.setInvocation(std::move(Invocation));
Compiler.createDiagnostics();
EXPECT_TRUE(Compiler.hasDiagnostics());
std::unique_ptr<FrontendAction> Act(new NullCodeGenAction);
bool Success = Compiler.ExecuteAction(*Act);
EXPECT_TRUE(Success);
}
}

View File

@ -0,0 +1,74 @@
//===- unittests/Frontend/CompilerInstanceTest.cpp - CI tests -------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/CompilerInvocation.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/ToolOutputFile.h"
#include "gtest/gtest.h"
using namespace llvm;
using namespace clang;
namespace {
TEST(CompilerInstance, DefaultVFSOverlayFromInvocation) {
// Create a temporary VFS overlay yaml file.
int FD;
SmallString<256> FileName;
ASSERT_FALSE(sys::fs::createTemporaryFile("vfs", "yaml", FD, FileName));
ToolOutputFile File(FileName, FD);
SmallString<256> CurrentPath;
sys::fs::current_path(CurrentPath);
sys::fs::make_absolute(CurrentPath, FileName);
// Mount the VFS file itself on the path 'virtual.file'. Makes this test
// a bit shorter than creating a new dummy file just for this purpose.
const std::string CurrentPathStr = CurrentPath.str();
const std::string FileNameStr = FileName.str();
const char *VFSYaml = "{ 'version': 0, 'roots': [\n"
" { 'name': '%s',\n"
" 'type': 'directory',\n"
" 'contents': [\n"
" { 'name': 'vfs-virtual.file', 'type': 'file',\n"
" 'external-contents': '%s'\n"
" }\n"
" ]\n"
" }\n"
"]}\n";
File.os() << format(VFSYaml, CurrentPathStr.c_str(), FileName.c_str());
File.os().flush();
// Create a CompilerInvocation that uses this overlay file.
const std::string VFSArg = "-ivfsoverlay" + FileNameStr;
const char *Args[] = {"clang", VFSArg.c_str(), "-xc++", "-"};
IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
CompilerInstance::createDiagnostics(new DiagnosticOptions());
std::shared_ptr<CompilerInvocation> CInvok =
createInvocationFromCommandLine(Args, Diags);
if (!CInvok)
FAIL() << "could not create compiler invocation";
// Create a minimal CompilerInstance which should use the VFS we specified
// in the CompilerInvocation (as we don't explicitly set our own).
CompilerInstance Instance;
Instance.setDiagnostics(Diags.get());
Instance.setInvocation(CInvok);
Instance.createFileManager();
// Check if the virtual file exists which means that our VFS is used by the
// CompilerInstance.
ASSERT_TRUE(Instance.getFileManager().getFile("vfs-virtual.file"));
}
} // anonymous namespace

View File

@ -0,0 +1,257 @@
//===- unittests/Frontend/FrontendActionTest.cpp - FrontendAction tests ---===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/CompilerInvocation.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/PreprocessorOptions.h"
#include "clang/Sema/Sema.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Support/MemoryBuffer.h"
#include "gtest/gtest.h"
using namespace llvm;
using namespace clang;
namespace {
class TestASTFrontendAction : public ASTFrontendAction {
public:
TestASTFrontendAction(bool enableIncrementalProcessing = false,
bool actOnEndOfTranslationUnit = false)
: EnableIncrementalProcessing(enableIncrementalProcessing),
ActOnEndOfTranslationUnit(actOnEndOfTranslationUnit) { }
bool EnableIncrementalProcessing;
bool ActOnEndOfTranslationUnit;
std::vector<std::string> decl_names;
bool BeginSourceFileAction(CompilerInstance &ci) override {
if (EnableIncrementalProcessing)
ci.getPreprocessor().enableIncrementalProcessing();
return ASTFrontendAction::BeginSourceFileAction(ci);
}
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
StringRef InFile) override {
return llvm::make_unique<Visitor>(CI, ActOnEndOfTranslationUnit,
decl_names);
}
private:
class Visitor : public ASTConsumer, public RecursiveASTVisitor<Visitor> {
public:
Visitor(CompilerInstance &CI, bool ActOnEndOfTranslationUnit,
std::vector<std::string> &decl_names) :
CI(CI), ActOnEndOfTranslationUnit(ActOnEndOfTranslationUnit),
decl_names_(decl_names) {}
void HandleTranslationUnit(ASTContext &context) override {
if (ActOnEndOfTranslationUnit) {
CI.getSema().ActOnEndOfTranslationUnit();
}
TraverseDecl(context.getTranslationUnitDecl());
}
virtual bool VisitNamedDecl(NamedDecl *Decl) {
decl_names_.push_back(Decl->getQualifiedNameAsString());
return true;
}
private:
CompilerInstance &CI;
bool ActOnEndOfTranslationUnit;
std::vector<std::string> &decl_names_;
};
};
TEST(ASTFrontendAction, Sanity) {
auto invocation = std::make_shared<CompilerInvocation>();
invocation->getPreprocessorOpts().addRemappedFile(
"test.cc",
MemoryBuffer::getMemBuffer("int main() { float x; }").release());
invocation->getFrontendOpts().Inputs.push_back(
FrontendInputFile("test.cc", InputKind::CXX));
invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
CompilerInstance compiler;
compiler.setInvocation(std::move(invocation));
compiler.createDiagnostics();
TestASTFrontendAction test_action;
ASSERT_TRUE(compiler.ExecuteAction(test_action));
ASSERT_EQ(2U, test_action.decl_names.size());
EXPECT_EQ("main", test_action.decl_names[0]);
EXPECT_EQ("x", test_action.decl_names[1]);
}
TEST(ASTFrontendAction, IncrementalParsing) {
auto invocation = std::make_shared<CompilerInvocation>();
invocation->getPreprocessorOpts().addRemappedFile(
"test.cc",
MemoryBuffer::getMemBuffer("int main() { float x; }").release());
invocation->getFrontendOpts().Inputs.push_back(
FrontendInputFile("test.cc", InputKind::CXX));
invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
CompilerInstance compiler;
compiler.setInvocation(std::move(invocation));
compiler.createDiagnostics();
TestASTFrontendAction test_action(/*enableIncrementalProcessing=*/true);
ASSERT_TRUE(compiler.ExecuteAction(test_action));
ASSERT_EQ(2U, test_action.decl_names.size());
EXPECT_EQ("main", test_action.decl_names[0]);
EXPECT_EQ("x", test_action.decl_names[1]);
}
TEST(ASTFrontendAction, LateTemplateIncrementalParsing) {
auto invocation = std::make_shared<CompilerInvocation>();
invocation->getLangOpts()->CPlusPlus = true;
invocation->getLangOpts()->DelayedTemplateParsing = true;
invocation->getPreprocessorOpts().addRemappedFile(
"test.cc", MemoryBuffer::getMemBuffer(
"template<typename T> struct A { A(T); T data; };\n"
"template<typename T> struct B: public A<T> {\n"
" B();\n"
" B(B const& b): A<T>(b.data) {}\n"
"};\n"
"B<char> c() { return B<char>(); }\n").release());
invocation->getFrontendOpts().Inputs.push_back(
FrontendInputFile("test.cc", InputKind::CXX));
invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
CompilerInstance compiler;
compiler.setInvocation(std::move(invocation));
compiler.createDiagnostics();
TestASTFrontendAction test_action(/*enableIncrementalProcessing=*/true,
/*actOnEndOfTranslationUnit=*/true);
ASSERT_TRUE(compiler.ExecuteAction(test_action));
ASSERT_EQ(13U, test_action.decl_names.size());
EXPECT_EQ("A", test_action.decl_names[0]);
EXPECT_EQ("c", test_action.decl_names[12]);
}
struct TestPPCallbacks : public PPCallbacks {
TestPPCallbacks() : SeenEnd(false) {}
void EndOfMainFile() override { SeenEnd = true; }
bool SeenEnd;
};
class TestPPCallbacksFrontendAction : public PreprocessorFrontendAction {
TestPPCallbacks *Callbacks;
public:
TestPPCallbacksFrontendAction(TestPPCallbacks *C)
: Callbacks(C), SeenEnd(false) {}
void ExecuteAction() override {
Preprocessor &PP = getCompilerInstance().getPreprocessor();
PP.addPPCallbacks(std::unique_ptr<TestPPCallbacks>(Callbacks));
PP.EnterMainSourceFile();
}
void EndSourceFileAction() override { SeenEnd = Callbacks->SeenEnd; }
bool SeenEnd;
};
TEST(PreprocessorFrontendAction, EndSourceFile) {
auto Invocation = std::make_shared<CompilerInvocation>();
Invocation->getPreprocessorOpts().addRemappedFile(
"test.cc",
MemoryBuffer::getMemBuffer("int main() { float x; }").release());
Invocation->getFrontendOpts().Inputs.push_back(
FrontendInputFile("test.cc", InputKind::CXX));
Invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
CompilerInstance Compiler;
Compiler.setInvocation(std::move(Invocation));
Compiler.createDiagnostics();
TestPPCallbacks *Callbacks = new TestPPCallbacks;
TestPPCallbacksFrontendAction TestAction(Callbacks);
ASSERT_FALSE(Callbacks->SeenEnd);
ASSERT_FALSE(TestAction.SeenEnd);
ASSERT_TRUE(Compiler.ExecuteAction(TestAction));
// Check that EndOfMainFile was called before EndSourceFileAction.
ASSERT_TRUE(TestAction.SeenEnd);
}
class TypoExternalSemaSource : public ExternalSemaSource {
CompilerInstance &CI;
public:
TypoExternalSemaSource(CompilerInstance &CI) : CI(CI) {}
TypoCorrection CorrectTypo(const DeclarationNameInfo &Typo, int LookupKind,
Scope *S, CXXScopeSpec *SS,
CorrectionCandidateCallback &CCC,
DeclContext *MemberContext, bool EnteringContext,
const ObjCObjectPointerType *OPT) override {
// Generate a fake typo correction with one attached note.
ASTContext &Ctx = CI.getASTContext();
TypoCorrection TC(DeclarationName(&Ctx.Idents.get("moo")));
unsigned DiagID = Ctx.getDiagnostics().getCustomDiagID(
DiagnosticsEngine::Note, "This is a note");
TC.addExtraDiagnostic(PartialDiagnostic(DiagID, Ctx.getDiagAllocator()));
return TC;
}
};
struct TypoDiagnosticConsumer : public DiagnosticConsumer {
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
const Diagnostic &Info) override {
// Capture errors and notes. There should be one of each.
if (DiagLevel == DiagnosticsEngine::Error) {
assert(Error.empty());
Info.FormatDiagnostic(Error);
} else {
assert(Note.empty());
Info.FormatDiagnostic(Note);
}
}
SmallString<32> Error;
SmallString<32> Note;
};
TEST(ASTFrontendAction, ExternalSemaSource) {
auto Invocation = std::make_shared<CompilerInvocation>();
Invocation->getLangOpts()->CPlusPlus = true;
Invocation->getPreprocessorOpts().addRemappedFile(
"test.cc", MemoryBuffer::getMemBuffer("void fooo();\n"
"int main() { foo(); }")
.release());
Invocation->getFrontendOpts().Inputs.push_back(
FrontendInputFile("test.cc", InputKind::CXX));
Invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
CompilerInstance Compiler;
Compiler.setInvocation(std::move(Invocation));
auto *TDC = new TypoDiagnosticConsumer;
Compiler.createDiagnostics(TDC, /*ShouldOwnClient=*/true);
Compiler.setExternalSemaSource(new TypoExternalSemaSource(Compiler));
SyntaxOnlyAction TestAction;
ASSERT_TRUE(Compiler.ExecuteAction(TestAction));
// There should be one error correcting to 'moo' and a note attached to it.
EXPECT_EQ("use of undeclared identifier 'foo'; did you mean 'moo'?",
TDC->Error.str().str());
EXPECT_EQ("This is a note", TDC->Note.str().str());
}
} // anonymous namespace

View File

@ -0,0 +1,200 @@
//====-- unittests/Frontend/PCHPreambleTest.cpp - FrontendAction tests ---====//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "clang/Frontend/ASTUnit.h"
#include "clang/Frontend/CompilerInvocation.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Frontend/FrontendOptions.h"
#include "clang/Lex/PreprocessorOptions.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/FileManager.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "gtest/gtest.h"
using namespace llvm;
using namespace clang;
namespace {
class ReadCountingInMemoryFileSystem : public vfs::InMemoryFileSystem
{
std::map<std::string, unsigned> ReadCounts;
public:
ErrorOr<std::unique_ptr<vfs::File>> openFileForRead(const Twine &Path) override
{
SmallVector<char, 128> PathVec;
Path.toVector(PathVec);
llvm::sys::path::remove_dots(PathVec, true);
++ReadCounts[std::string(PathVec.begin(), PathVec.end())];
return InMemoryFileSystem::openFileForRead(Path);
}
unsigned GetReadCount(const Twine &Path) const
{
auto it = ReadCounts.find(Path.str());
return it == ReadCounts.end() ? 0 : it->second;
}
};
class PCHPreambleTest : public ::testing::Test {
IntrusiveRefCntPtr<ReadCountingInMemoryFileSystem> VFS;
StringMap<std::string> RemappedFiles;
std::shared_ptr<PCHContainerOperations> PCHContainerOpts;
FileSystemOptions FSOpts;
public:
void SetUp() override {
VFS = new ReadCountingInMemoryFileSystem();
// We need the working directory to be set to something absolute,
// otherwise it ends up being inadvertently set to the current
// working directory in the real file system due to a series of
// unfortunate conditions interacting badly.
// What's more, this path *must* be absolute on all (real)
// filesystems, so just '/' won't work (e.g. on Win32).
VFS->setCurrentWorkingDirectory("//./");
}
void TearDown() override {
}
void AddFile(const std::string &Filename, const std::string &Contents) {
::time_t now;
::time(&now);
VFS->addFile(Filename, now, MemoryBuffer::getMemBufferCopy(Contents, Filename));
}
void RemapFile(const std::string &Filename, const std::string &Contents) {
RemappedFiles[Filename] = Contents;
}
std::unique_ptr<ASTUnit> ParseAST(const std::string &EntryFile) {
PCHContainerOpts = std::make_shared<PCHContainerOperations>();
std::shared_ptr<CompilerInvocation> CI(new CompilerInvocation);
CI->getFrontendOpts().Inputs.push_back(
FrontendInputFile(EntryFile, FrontendOptions::getInputKindForExtension(
llvm::sys::path::extension(EntryFile).substr(1))));
CI->getTargetOpts().Triple = "i386-unknown-linux-gnu";
CI->getPreprocessorOpts().RemappedFileBuffers = GetRemappedFiles();
PreprocessorOptions &PPOpts = CI->getPreprocessorOpts();
PPOpts.RemappedFilesKeepOriginalName = true;
IntrusiveRefCntPtr<DiagnosticsEngine>
Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions, new DiagnosticConsumer));
FileManager *FileMgr = new FileManager(FSOpts, VFS);
std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCompilerInvocation(
CI, PCHContainerOpts, Diags, FileMgr, false, false,
/*PrecompilePreambleAfterNParses=*/1);
return AST;
}
bool ReparseAST(const std::unique_ptr<ASTUnit> &AST) {
bool reparseFailed = AST->Reparse(PCHContainerOpts, GetRemappedFiles(), VFS);
return !reparseFailed;
}
unsigned GetFileReadCount(const std::string &Filename) const {
return VFS->GetReadCount(Filename);
}
private:
std::vector<std::pair<std::string, llvm::MemoryBuffer *>>
GetRemappedFiles() const {
std::vector<std::pair<std::string, llvm::MemoryBuffer *>> Remapped;
for (const auto &RemappedFile : RemappedFiles) {
std::unique_ptr<MemoryBuffer> buf = MemoryBuffer::getMemBufferCopy(
RemappedFile.second, RemappedFile.first());
Remapped.emplace_back(RemappedFile.first(), buf.release());
}
return Remapped;
}
};
TEST_F(PCHPreambleTest, ReparseWithOverriddenFileDoesNotInvalidatePreamble) {
std::string Header1 = "//./header1.h";
std::string Header2 = "//./header2.h";
std::string MainName = "//./main.cpp";
AddFile(Header1, "");
AddFile(Header2, "#pragma once");
AddFile(MainName,
"#include \"//./foo/../header1.h\"\n"
"#include \"//./foo/../header2.h\"\n"
"int main() { return ZERO; }");
RemapFile(Header1, "static const int ZERO = 0;\n");
std::unique_ptr<ASTUnit> AST(ParseAST(MainName));
ASSERT_TRUE(AST.get());
ASSERT_FALSE(AST->getDiagnostics().hasErrorOccurred());
unsigned initialCounts[] = {
GetFileReadCount(MainName),
GetFileReadCount(Header1),
GetFileReadCount(Header2)
};
ASSERT_TRUE(ReparseAST(AST));
ASSERT_NE(initialCounts[0], GetFileReadCount(MainName));
ASSERT_EQ(initialCounts[1], GetFileReadCount(Header1));
ASSERT_EQ(initialCounts[2], GetFileReadCount(Header2));
}
TEST_F(PCHPreambleTest, ParseWithBom) {
std::string Header = "//./header.h";
std::string Main = "//./main.cpp";
AddFile(Header, "int random() { return 4; }");
AddFile(Main,
"\xef\xbb\xbf"
"#include \"//./header.h\"\n"
"int main() { return random() -2; }");
std::unique_ptr<ASTUnit> AST(ParseAST(Main));
ASSERT_TRUE(AST.get());
ASSERT_FALSE(AST->getDiagnostics().hasErrorOccurred());
unsigned HeaderReadCount = GetFileReadCount(Header);
ASSERT_TRUE(ReparseAST(AST));
ASSERT_FALSE(AST->getDiagnostics().hasErrorOccurred());
// Check preamble PCH was really reused
ASSERT_EQ(HeaderReadCount, GetFileReadCount(Header));
// Remove BOM
RemapFile(Main,
"#include \"//./header.h\"\n"
"int main() { return random() -2; }");
ASSERT_TRUE(ReparseAST(AST));
ASSERT_FALSE(AST->getDiagnostics().hasErrorOccurred());
ASSERT_LE(HeaderReadCount, GetFileReadCount(Header));
HeaderReadCount = GetFileReadCount(Header);
// Add BOM back
RemapFile(Main,
"\xef\xbb\xbf"
"#include \"//./header.h\"\n"
"int main() { return random() -2; }");
ASSERT_TRUE(ReparseAST(AST));
ASSERT_FALSE(AST->getDiagnostics().hasErrorOccurred());
ASSERT_LE(HeaderReadCount, GetFileReadCount(Header));
}
} // anonymous namespace

View File

@ -0,0 +1,37 @@
//===- unittests/Frontend/ParsedSourceLocationTest.cpp - ------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "clang/Frontend/CommandLineSourceLoc.h"
#include "gtest/gtest.h"
using namespace llvm;
using namespace clang;
namespace {
TEST(ParsedSourceRange, ParseTest) {
auto Check = [](StringRef Value, StringRef Filename, unsigned BeginLine,
unsigned BeginColumn, unsigned EndLine, unsigned EndColumn) {
Optional<ParsedSourceRange> PSR = ParsedSourceRange::fromString(Value);
ASSERT_TRUE(PSR);
EXPECT_EQ(PSR->FileName, Filename);
EXPECT_EQ(PSR->Begin.first, BeginLine);
EXPECT_EQ(PSR->Begin.second, BeginColumn);
EXPECT_EQ(PSR->End.first, EndLine);
EXPECT_EQ(PSR->End.second, EndColumn);
};
Check("/Users/test/a-b.cpp:1:2", "/Users/test/a-b.cpp", 1, 2, 1, 2);
Check("/Users/test/a-b.cpp:1:2-3:4", "/Users/test/a-b.cpp", 1, 2, 3, 4);
Check("C:/Users/bob/a-b.cpp:1:2", "C:/Users/bob/a-b.cpp", 1, 2, 1, 2);
Check("C:/Users/bob/a-b.cpp:1:2-3:4", "C:/Users/bob/a-b.cpp", 1, 2, 3, 4);
}
} // anonymous namespace