Skip to content

Commit 3219fb0

Browse files
authored
[clang][HeuristicResolver] Test suite: fail if test code does't compile (llvm#155561)
Fixes llvm#155545
1 parent b2a7369 commit 3219fb0

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

clang/include/clang/Frontend/ASTUnit.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,17 @@ class ASTUnit {
629629
return StoredDiagnostics.end();
630630
}
631631

632+
using diags_range = llvm::iterator_range<stored_diag_iterator>;
633+
using const_diags_range = llvm::iterator_range<stored_diag_const_iterator>;
634+
635+
diags_range storedDiagnostics() {
636+
return {stored_diag_begin(), stored_diag_end()};
637+
}
638+
639+
const_diags_range storedDiagnostics() const {
640+
return {stored_diag_begin(), stored_diag_end()};
641+
}
642+
632643
unsigned stored_diag_size() const { return StoredDiagnostics.size(); }
633644

634645
stored_diag_iterator stored_diag_afterDriver_begin() {

clang/include/clang/Tooling/Tooling.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "clang/AST/ASTConsumer.h"
3333
#include "clang/Basic/FileManager.h"
3434
#include "clang/Basic/LLVM.h"
35+
#include "clang/Frontend/ASTUnit.h"
3536
#include "clang/Frontend/FrontendAction.h"
3637
#include "clang/Frontend/PCHContainerOperations.h"
3738
#include "clang/Tooling/ArgumentsAdjusters.h"
@@ -239,7 +240,8 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
239240
const FileContentMappings &VirtualMappedFiles = FileContentMappings(),
240241
DiagnosticConsumer *DiagConsumer = nullptr,
241242
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS =
242-
llvm::vfs::getRealFileSystem());
243+
llvm::vfs::getRealFileSystem(),
244+
CaptureDiagsKind CaptureKind = CaptureDiagsKind::None);
243245

244246
/// Utility to run a FrontendAction in a single clang invocation.
245247
class ToolInvocation {

clang/lib/Tooling/Tooling.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -644,9 +644,13 @@ namespace {
644644

645645
class ASTBuilderAction : public ToolAction {
646646
std::vector<std::unique_ptr<ASTUnit>> &ASTs;
647+
CaptureDiagsKind CaptureKind;
647648

648649
public:
649-
ASTBuilderAction(std::vector<std::unique_ptr<ASTUnit>> &ASTs) : ASTs(ASTs) {}
650+
ASTBuilderAction(
651+
std::vector<std::unique_ptr<ASTUnit>> &ASTs,
652+
CaptureDiagsKind CaptureDiagnosticsKind = CaptureDiagsKind::None)
653+
: ASTs(ASTs), CaptureKind(CaptureDiagnosticsKind) {}
650654

651655
bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
652656
FileManager *Files,
@@ -658,7 +662,7 @@ class ASTBuilderAction : public ToolAction {
658662
Invocation->getDiagnosticOpts(),
659663
DiagConsumer,
660664
/*ShouldOwnClient=*/false),
661-
Files);
665+
Files, false, CaptureKind);
662666
if (!AST)
663667
return false;
664668

@@ -693,9 +697,12 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
693697
StringRef ToolName, std::shared_ptr<PCHContainerOperations> PCHContainerOps,
694698
ArgumentsAdjuster Adjuster, const FileContentMappings &VirtualMappedFiles,
695699
DiagnosticConsumer *DiagConsumer,
696-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS) {
700+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
701+
CaptureDiagsKind CaptureKind) {
697702
std::vector<std::unique_ptr<ASTUnit>> ASTs;
698-
ASTBuilderAction Action(ASTs);
703+
704+
ASTBuilderAction Action(ASTs, CaptureKind);
705+
699706
auto OverlayFileSystem =
700707
llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(
701708
std::move(BaseFS));

clang/unittests/Sema/HeuristicResolverTest.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
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"
1213
#include "gmock/gmock-matchers.h"
1314
#include "gtest/gtest.h"
@@ -41,7 +42,19 @@ template <typename InputNode, typename ParamT, typename InputMatcher,
4142
typename... OutputMatchers>
4243
void expectResolution(llvm::StringRef Code, ResolveFnT<ParamT> ResolveFn,
4344
const InputMatcher &IM, const OutputMatchers &...OMS) {
44-
auto TU = tooling::buildASTFromCodeWithArgs(Code, {"-std=c++23"});
45+
auto TU = tooling::buildASTFromCodeWithArgs(
46+
Code, {"-std=c++23"}, "input.cc", "clang-tool",
47+
std::make_shared<PCHContainerOperations>(),
48+
tooling::getClangStripDependencyFileAdjuster(),
49+
tooling::FileContentMappings(), nullptr, llvm::vfs::getRealFileSystem(),
50+
CaptureDiagsKind::All);
51+
52+
for (const auto &D : TU->storedDiagnostics()) {
53+
EXPECT_TRUE(D.getLevel() < DiagnosticsEngine::Error)
54+
<< "Unexpected error diagnostic while building AST for test code: "
55+
<< D.getMessage();
56+
}
57+
4558
auto &Ctx = TU->getASTContext();
4659
auto InputMatches = match(IM, Ctx);
4760
ASSERT_EQ(1u, InputMatches.size());

0 commit comments

Comments
 (0)