teak-llvm/clang-tools-extra/clangd/unittests/TestTU.cpp
Sam McCall d3260bf5b2 [clangd] Errors in TestTU cause test failures unless suppressed with error-ok.
Summary:
The historic behavior of TestTU is to gather diagnostics and otherwise ignore
them. So if a test has a syntax error, and doesn't assert diagnostics, it
silently misbehaves.
This can be annoying when developing tests, as evidenced by various tests
gaining "assert no diagnostics" where that's not really the point of the test.

This patch aims to make that default behavior. For the first error
(not warning), TestTU will call ADD_FAILURE().

This can be suppressed with a comment containing "error-ok". For now that will
suppress any errors in the TU. We can make this stricter later -verify style.
(-verify itself is hard to reuse because of DiagnosticConsumer interfaces...)
A magic-comment was chosen over a TestTU option because of table-driven tests.

In addition to the behavior change, this patch:
  - adds //error-ok where we're knowingly testing invalid code
    (e.g. for diagnostics, crash-resilience, or token-level tests)
  - fixes a bunch of errors in the checked-in tests, mostly trivial (missing ;)
  - removes a bunch of now-redundant instances of "assert no diagnostics"

Reviewers: kadircet

Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D73199
2020-01-24 11:16:27 +01:00

187 lines
6.9 KiB
C++

//===--- TestTU.cpp - Scratch source files for testing --------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "TestTU.h"
#include "Compiler.h"
#include "Diagnostics.h"
#include "TestFS.h"
#include "index/FileIndex.h"
#include "index/MemIndex.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Frontend/CompilerInvocation.h"
#include "clang/Frontend/Utils.h"
namespace clang {
namespace clangd {
ParsedAST TestTU::build() const {
std::string FullFilename = testPath(Filename),
FullHeaderName = testPath(HeaderFilename),
ImportThunk = testPath("import_thunk.h");
// We want to implicitly include HeaderFilename without messing up offsets.
// -include achieves this, but sometimes we want #import (to simulate a header
// guard without messing up offsets). In this case, use an intermediate file.
std::string ThunkContents = "#import \"" + FullHeaderName + "\"\n";
llvm::StringMap<std::string> Files(AdditionalFiles);
Files[FullFilename] = Code;
Files[FullHeaderName] = HeaderCode;
Files[ImportThunk] = ThunkContents;
std::vector<const char *> Cmd = {"clang"};
// FIXME: this shouldn't need to be conditional, but it breaks a
// GoToDefinition test for some reason (getMacroArgExpandedLocation fails).
if (!HeaderCode.empty()) {
Cmd.push_back("-include");
Cmd.push_back(ImplicitHeaderGuard ? ImportThunk.c_str()
: FullHeaderName.c_str());
// ms-compatibility changes the meaning of #import.
// The default is OS-dependent (on on windows), ensure it's off.
if (ImplicitHeaderGuard)
Cmd.push_back("-fno-ms-compatibility");
}
Cmd.insert(Cmd.end(), ExtraArgs.begin(), ExtraArgs.end());
// Put the file name at the end -- this allows the extra arg (-xc++) to
// override the language setting.
Cmd.push_back(FullFilename.c_str());
ParseInputs Inputs;
Inputs.CompileCommand.Filename = FullFilename;
Inputs.CompileCommand.CommandLine = {Cmd.begin(), Cmd.end()};
Inputs.CompileCommand.Directory = testRoot();
Inputs.Contents = Code;
Inputs.FS = buildTestFS(Files);
Inputs.Opts = ParseOptions();
Inputs.Opts.ClangTidyOpts.Checks = ClangTidyChecks;
Inputs.Opts.ClangTidyOpts.WarningsAsErrors = ClangTidyWarningsAsErrors;
Inputs.Index = ExternalIndex;
if (Inputs.Index)
Inputs.Opts.SuggestMissingIncludes = true;
StoreDiags Diags;
auto CI = buildCompilerInvocation(Inputs, Diags);
assert(CI && "Failed to build compilation invocation.");
auto Preamble =
buildPreamble(FullFilename, *CI,
/*OldPreamble=*/nullptr,
/*OldCompileCommand=*/Inputs.CompileCommand, Inputs,
/*StoreInMemory=*/true, /*PreambleCallback=*/nullptr);
auto AST =
buildAST(FullFilename, std::move(CI), Diags.take(), Inputs, Preamble);
if (!AST.hasValue()) {
ADD_FAILURE() << "Failed to build code:\n" << Code;
llvm_unreachable("Failed to build TestTU!");
}
// Check for error diagnostics and report gtest failures (unless expected).
// This guards against accidental syntax errors silently subverting tests.
// error-ok is awfully primitive - using clang -verify would be nicer.
// Ownership and layering makes it pretty hard.
if (llvm::none_of(Files, [](const auto &KV) {
return llvm::StringRef(KV.second).contains("error-ok");
})) {
for (const auto &D : AST->getDiagnostics())
if (D.Severity >= DiagnosticsEngine::Error) {
ADD_FAILURE()
<< "TestTU failed to build (suppress with /*error-ok*/): \n"
<< D << "\n\nFor code:\n"
<< Code;
break; // Just report first error for simplicity.
}
}
return std::move(*AST);
}
SymbolSlab TestTU::headerSymbols() const {
auto AST = build();
return std::get<0>(indexHeaderSymbols(AST.getASTContext(),
AST.getPreprocessorPtr(),
AST.getCanonicalIncludes()));
}
std::unique_ptr<SymbolIndex> TestTU::index() const {
auto AST = build();
auto Idx = std::make_unique<FileIndex>(/*UseDex=*/true);
Idx->updatePreamble(Filename, AST.getASTContext(), AST.getPreprocessorPtr(),
AST.getCanonicalIncludes());
Idx->updateMain(Filename, AST);
return std::move(Idx);
}
const Symbol &findSymbol(const SymbolSlab &Slab, llvm::StringRef QName) {
const Symbol *Result = nullptr;
for (const Symbol &S : Slab) {
if (QName != (S.Scope + S.Name).str())
continue;
if (Result) {
ADD_FAILURE() << "Multiple symbols named " << QName << ":\n"
<< *Result << "\n---\n"
<< S;
assert(false && "QName is not unique");
}
Result = &S;
}
if (!Result) {
ADD_FAILURE() << "No symbol named " << QName << " in "
<< ::testing::PrintToString(Slab);
assert(false && "No symbol with QName");
}
return *Result;
}
const NamedDecl &findDecl(ParsedAST &AST, llvm::StringRef QName) {
llvm::SmallVector<llvm::StringRef, 4> Components;
QName.split(Components, "::");
auto &Ctx = AST.getASTContext();
auto LookupDecl = [&Ctx](const DeclContext &Scope,
llvm::StringRef Name) -> const NamedDecl & {
auto LookupRes = Scope.lookup(DeclarationName(&Ctx.Idents.get(Name)));
assert(!LookupRes.empty() && "Lookup failed");
assert(LookupRes.size() == 1 && "Lookup returned multiple results");
return *LookupRes.front();
};
const DeclContext *Scope = Ctx.getTranslationUnitDecl();
for (auto NameIt = Components.begin(), End = Components.end() - 1;
NameIt != End; ++NameIt) {
Scope = &cast<DeclContext>(LookupDecl(*Scope, *NameIt));
}
return LookupDecl(*Scope, Components.back());
}
const NamedDecl &findDecl(ParsedAST &AST,
std::function<bool(const NamedDecl &)> Filter) {
struct Visitor : RecursiveASTVisitor<Visitor> {
decltype(Filter) F;
llvm::SmallVector<const NamedDecl *, 1> Decls;
bool VisitNamedDecl(const NamedDecl *ND) {
if (F(*ND))
Decls.push_back(ND);
return true;
}
} Visitor;
Visitor.F = Filter;
Visitor.TraverseDecl(AST.getASTContext().getTranslationUnitDecl());
if (Visitor.Decls.size() != 1) {
ADD_FAILURE() << Visitor.Decls.size() << " symbols matched.";
assert(Visitor.Decls.size() == 1);
}
return *Visitor.Decls.front();
}
const NamedDecl &findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name) {
return findDecl(AST, [Name](const NamedDecl &ND) {
if (auto *ID = ND.getIdentifier())
if (ID->getName() == Name)
return true;
return false;
});
}
} // namespace clangd
} // namespace clang