[clang-tools-extra] 4728aca - [clangd] Drop TestTUs dependency on gtest
Kadir Cetinkaya via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 7 04:25:50 PDT 2021
Author: Kadir Cetinkaya
Date: 2021-06-07T13:25:22+02:00
New Revision: 4728aca9a8adadc34590e3c930dcf8a32593d846
URL: https://github.com/llvm/llvm-project/commit/4728aca9a8adadc34590e3c930dcf8a32593d846
DIFF: https://github.com/llvm/llvm-project/commit/4728aca9a8adadc34590e3c930dcf8a32593d846.diff
LOG: [clangd] Drop TestTUs dependency on gtest
TestTU now prints errors to llvm::errs and aborts on failures via
llvm_unreachable, rather than executing ASSERT_FALSE.
We'd like to make use of these testing libraries in different test suits that
might be compiling with a different gtest version than LLVM has.
Differential Revision: https://reviews.llvm.org/D103685
Added:
Modified:
clang-tools-extra/clangd/index/Symbol.cpp
clang-tools-extra/clangd/index/Symbol.h
clang-tools-extra/clangd/unittests/TestTU.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/index/Symbol.cpp b/clang-tools-extra/clangd/index/Symbol.cpp
index 4d9fdca6fe9dd..55d61826097d2 100644
--- a/clang-tools-extra/clangd/index/Symbol.cpp
+++ b/clang-tools-extra/clangd/index/Symbol.cpp
@@ -67,5 +67,15 @@ SymbolSlab SymbolSlab::Builder::build() && {
return SymbolSlab(std::move(NewArena), std::move(SortedSymbols));
}
+llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SymbolSlab &Slab) {
+ OS << "{";
+ llvm::StringRef Sep = "";
+ for (const auto &S : Slab) {
+ OS << Sep << S;
+ Sep = ", ";
+ }
+ OS << "}";
+ return OS;
+}
} // namespace clangd
} // namespace clang
diff --git a/clang-tools-extra/clangd/index/Symbol.h b/clang-tools-extra/clangd/index/Symbol.h
index 2d2644e31a03d..5768877de895e 100644
--- a/clang-tools-extra/clangd/index/Symbol.h
+++ b/clang-tools-extra/clangd/index/Symbol.h
@@ -233,6 +233,8 @@ class SymbolSlab {
std::vector<Symbol> Symbols; // Sorted by SymbolID to allow lookup.
};
+llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SymbolSlab &Slab);
+
} // namespace clangd
} // namespace clang
diff --git a/clang-tools-extra/clangd/unittests/TestTU.cpp b/clang-tools-extra/clangd/unittests/TestTU.cpp
index 36f4290bd5a3a..335993a18d6f0 100644
--- a/clang-tools-extra/clangd/unittests/TestTU.cpp
+++ b/clang-tools-extra/clangd/unittests/TestTU.cpp
@@ -17,7 +17,9 @@
#include "clang/Frontend/CompilerInvocation.h"
#include "clang/Frontend/Utils.h"
#include "llvm/ADT/ScopeExit.h"
-#include "gtest/gtest.h"
+#include "llvm/Support/ScopedPrinter.h"
+#include "llvm/Support/raw_ostream.h"
+#include <cstdlib>
namespace clang {
namespace clangd {
@@ -69,14 +71,19 @@ ParseInputs TestTU::inputs(MockFS &FS) const {
void initializeModuleCache(CompilerInvocation &CI) {
llvm::SmallString<128> ModuleCachePath;
- ASSERT_FALSE(
- llvm::sys::fs::createUniqueDirectory("module-cache", ModuleCachePath));
+ if (llvm::sys::fs::createUniqueDirectory("module-cache", ModuleCachePath)) {
+ llvm::errs() << "Failed to create temp directory for module-cache";
+ std::abort();
+ }
CI.getHeaderSearchOpts().ModuleCachePath = ModuleCachePath.c_str();
}
void deleteModuleCache(const std::string ModuleCachePath) {
if (!ModuleCachePath.empty()) {
- ASSERT_FALSE(llvm::sys::fs::remove_directories(ModuleCachePath));
+ if (llvm::sys::fs::remove_directories(ModuleCachePath)) {
+ llvm::errs() << "Failed to delete temp directory for module-cache";
+ std::abort();
+ }
}
}
@@ -112,14 +119,11 @@ ParsedAST TestTU::build() const {
auto AST = ParsedAST::build(testPath(Filename), Inputs, std::move(CI),
Diags.take(), Preamble);
if (!AST.hasValue()) {
- ADD_FAILURE() << "Failed to build code:\n" << Code;
- llvm_unreachable("Failed to build TestTU!");
- }
- if (!AST->getDiagnostics()) {
- ADD_FAILURE() << "TestTU should always build an AST with a fresh Preamble"
- << Code;
- return std::move(*AST);
+ llvm::errs() << "Failed to build code:\n" << Code;
+ std::abort();
}
+ assert(AST->getDiagnostics() &&
+ "TestTU should always build an AST with a fresh Preamble");
// 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.
@@ -138,11 +142,11 @@ ParsedAST TestTU::build() const {
// We always build AST with a fresh preamble in TestTU.
for (const auto &D : *AST->getDiagnostics())
if (D.Severity >= DiagnosticsEngine::Error) {
- ADD_FAILURE()
+ llvm::errs()
<< "TestTU failed to build (suppress with /*error-ok*/): \n"
<< D << "\n\nFor code:\n"
<< Code;
- break; // Just report first error for simplicity.
+ std::abort(); // Stop after first error for simplicity.
}
}
return std::move(*AST);
@@ -176,16 +180,16 @@ const Symbol &findSymbol(const SymbolSlab &Slab, llvm::StringRef QName) {
if (QName != (S.Scope + S.Name).str())
continue;
if (Result) {
- ADD_FAILURE() << "Multiple symbols named " << QName << ":\n"
- << *Result << "\n---\n"
- << S;
+ llvm::errs() << "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);
+ llvm::errs() << "No symbol named " << QName << " in "
+ << llvm::to_string(Slab);
assert(false && "No symbol with QName");
}
return *Result;
@@ -225,7 +229,7 @@ const NamedDecl &findDecl(ParsedAST &AST,
Visitor.F = Filter;
Visitor.TraverseDecl(AST.getASTContext().getTranslationUnitDecl());
if (Visitor.Decls.size() != 1) {
- ADD_FAILURE() << Visitor.Decls.size() << " symbols matched.";
+ llvm::errs() << Visitor.Decls.size() << " symbols matched.";
assert(Visitor.Decls.size() == 1);
}
return *Visitor.Decls.front();
More information about the cfe-commits
mailing list