Skip to content

Commit b1a30c8

Browse files
committed
Add error check in HeuristicResolverTest.cpp
1 parent b0c182f commit b1a30c8

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

clang/lib/Tooling/Tooling.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,13 +658,15 @@ class ASTBuilderAction : public ToolAction {
658658
Invocation->getDiagnosticOpts(),
659659
DiagConsumer,
660660
/*ShouldOwnClient=*/false),
661-
Files);
661+
Files, false, CaptureKinds);
662662
if (!AST)
663663
return false;
664664

665665
ASTs.push_back(std::move(AST));
666666
return true;
667667
}
668+
669+
CaptureDiagsKind CaptureKinds{CaptureDiagsKind::None};
668670
};
669671

670672
} // namespace
@@ -695,7 +697,10 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
695697
DiagnosticConsumer *DiagConsumer, CaptureDiagsKind CaptureKind,
696698
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS) {
697699
std::vector<std::unique_ptr<ASTUnit>> ASTs;
700+
698701
ASTBuilderAction Action(ASTs);
702+
Action.CaptureKinds = CaptureDiagsKind::All;
703+
699704
auto OverlayFileSystem =
700705
llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(
701706
std::move(BaseFS));

clang/unittests/Sema/HeuristicResolverTest.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
#include "clang/Sema/HeuristicResolver.h"
99
#include "clang/ASTMatchers/ASTMatchFinder.h"
1010
#include "clang/ASTMatchers/ASTMatchers.h"
11+
#include "clang/Basic/Diagnostic.h"
1112
#include "clang/Tooling/Tooling.h"
13+
#include "llvm/ADT/SmallSet.h"
1214
#include "gmock/gmock-matchers.h"
1315
#include "gtest/gtest.h"
1416

@@ -31,6 +33,24 @@ template <typename InputNode>
3133
using ResolveFnT = std::function<std::vector<const NamedDecl *>(
3234
const HeuristicResolver *, InputNode)>;
3335

36+
std::string format_error(const clang::StoredDiagnostic *D) {
37+
std::ostringstream Msg{};
38+
if (D->getLevel() == DiagnosticsEngine::Level::Ignored)
39+
Msg << "Ignored: ";
40+
if (D->getLevel() == DiagnosticsEngine::Level::Note)
41+
Msg << "Note: ";
42+
if (D->getLevel() == DiagnosticsEngine::Level::Remark)
43+
Msg << "Remark: ";
44+
if (D->getLevel() == DiagnosticsEngine::Level::Warning)
45+
Msg << "Warning: ";
46+
if (D->getLevel() == DiagnosticsEngine::Level::Error)
47+
Msg << "Error: ";
48+
if (D->getLevel() == DiagnosticsEngine::Level::Fatal)
49+
Msg << "Fatal: ";
50+
Msg << D->getID() << ": " << D->getMessage().str();
51+
return Msg.str();
52+
}
53+
3454
// Test heuristic resolution on `Code` using the resolution procedure
3555
// `ResolveFn`, which takes a `HeuristicResolver` and an input AST node of type
3656
// `InputNode` and returns a `std::vector<const NamedDecl *>`.
@@ -41,7 +61,16 @@ template <typename InputNode, typename ParamT, typename InputMatcher,
4161
typename... OutputMatchers>
4262
void expectResolution(llvm::StringRef Code, ResolveFnT<ParamT> ResolveFn,
4363
const InputMatcher &IM, const OutputMatchers &...OMS) {
64+
llvm::SmallSet<unsigned int, 16> IgnoredDiagnostics{};
4465
auto TU = tooling::buildASTFromCodeWithArgs(Code, {"-std=c++23"});
66+
67+
for (auto D = TU->stored_diag_begin(), DEnd = TU->stored_diag_end();
68+
D != DEnd; ++D) {
69+
EXPECT_TRUE(D->getLevel() < DiagnosticsEngine::Warning ||
70+
IgnoredDiagnostics.contains(D->getID()))
71+
<< format_error(D);
72+
}
73+
4574
auto &Ctx = TU->getASTContext();
4675
auto InputMatches = match(IM, Ctx);
4776
ASSERT_EQ(1u, InputMatches.size());

0 commit comments

Comments
 (0)