Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions clang/include/clang/Frontend/ASTUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,17 @@ class ASTUnit {
return StoredDiagnostics.end();
}

using diags_range = llvm::iterator_range<stored_diag_iterator>;
using const_diags_range = llvm::iterator_range<stored_diag_const_iterator>;

diags_range storedDiagnostics() {
return {stored_diag_begin(), stored_diag_end()};
}

const_diags_range storedDiagnostics() const {
return {stored_diag_begin(), stored_diag_end()};
}

unsigned stored_diag_size() const { return StoredDiagnostics.size(); }

stored_diag_iterator stored_diag_afterDriver_begin() {
Expand Down
4 changes: 3 additions & 1 deletion clang/include/clang/Tooling/Tooling.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "clang/AST/ASTConsumer.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/LLVM.h"
#include "clang/Frontend/ASTUnit.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Frontend/PCHContainerOperations.h"
#include "clang/Tooling/ArgumentsAdjusters.h"
Expand Down Expand Up @@ -239,7 +240,8 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
const FileContentMappings &VirtualMappedFiles = FileContentMappings(),
DiagnosticConsumer *DiagConsumer = nullptr,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS =
llvm::vfs::getRealFileSystem());
llvm::vfs::getRealFileSystem(),
CaptureDiagsKind CaptureKind = CaptureDiagsKind::None);

/// Utility to run a FrontendAction in a single clang invocation.
class ToolInvocation {
Expand Down
15 changes: 11 additions & 4 deletions clang/lib/Tooling/Tooling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,9 +644,13 @@ namespace {

class ASTBuilderAction : public ToolAction {
std::vector<std::unique_ptr<ASTUnit>> &ASTs;
CaptureDiagsKind CaptureKinds;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: CaptureKind or CaptureDiags (Kinds, as in "more than one kind", does not make sense here)


public:
ASTBuilderAction(std::vector<std::unique_ptr<ASTUnit>> &ASTs) : ASTs(ASTs) {}
ASTBuilderAction(
std::vector<std::unique_ptr<ASTUnit>> &ASTs,
CaptureDiagsKind CaptureDiagnosticKinds = CaptureDiagsKind::None)
: ASTs(ASTs), CaptureKinds(CaptureDiagnosticKinds) {}

bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
FileManager *Files,
Expand All @@ -658,7 +662,7 @@ class ASTBuilderAction : public ToolAction {
Invocation->getDiagnosticOpts(),
DiagConsumer,
/*ShouldOwnClient=*/false),
Files);
Files, false, CaptureKinds);
if (!AST)
return false;

Expand Down Expand Up @@ -693,9 +697,12 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
StringRef ToolName, std::shared_ptr<PCHContainerOperations> PCHContainerOps,
ArgumentsAdjuster Adjuster, const FileContentMappings &VirtualMappedFiles,
DiagnosticConsumer *DiagConsumer,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS) {
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
CaptureDiagsKind CaptureKind) {
std::vector<std::unique_ptr<ASTUnit>> ASTs;
ASTBuilderAction Action(ASTs);

ASTBuilderAction Action(ASTs, CaptureKind);

auto OverlayFileSystem =
llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(
std::move(BaseFS));
Expand Down
25 changes: 25 additions & 0 deletions clang/unittests/Sema/HeuristicResolverTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#include "clang/Sema/HeuristicResolver.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/SmallSet.h"
#include "gmock/gmock-matchers.h"
#include "gtest/gtest.h"

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

std::string format_error(const clang::StoredDiagnostic &D) {
std::ostringstream Msg{};
if (D.getLevel() == DiagnosticsEngine::Level::Ignored)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't actually need the first four cases, and I'm not sure the distinction between the remaining two (Error vs. Fatal) is that important; we could just skip printing the level?

On the other hand, we could print a short message prefix that provides some context, such as "Unexpected error diagnostic while building AST for test code:".

Msg << "Ignored: ";
if (D.getLevel() == DiagnosticsEngine::Level::Note)
Msg << "Note: ";
if (D.getLevel() == DiagnosticsEngine::Level::Remark)
Msg << "Remark: ";
if (D.getLevel() == DiagnosticsEngine::Level::Warning)
Msg << "Warning: ";
if (D.getLevel() == DiagnosticsEngine::Level::Error)
Msg << "Error: ";
if (D.getLevel() == DiagnosticsEngine::Level::Fatal)
Msg << "Fatal: ";
Msg << D.getID() << ": " << D.getMessage().str();
return Msg.str();
}

// Test heuristic resolution on `Code` using the resolution procedure
// `ResolveFn`, which takes a `HeuristicResolver` and an input AST node of type
// `InputNode` and returns a `std::vector<const NamedDecl *>`.
Expand All @@ -42,6 +62,11 @@ template <typename InputNode, typename ParamT, typename InputMatcher,
void expectResolution(llvm::StringRef Code, ResolveFnT<ParamT> ResolveFn,
const InputMatcher &IM, const OutputMatchers &...OMS) {
auto TU = tooling::buildASTFromCodeWithArgs(Code, {"-std=c++23"});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to be actually passing CaptureDiagsKind::All here

(A good way to validate that the mechanism we're adding is actually working is to change the -std=c++23 to -std=c++20 here. That should cause MemberExpr_ExplicitObjectParameter to fail, and it currently doesn't.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

omg totally forgot about this, sorry!


for (const auto &D : TU->storedDiagnostics()) {
EXPECT_TRUE(D.getLevel() < DiagnosticsEngine::Error) << format_error(D);
}

auto &Ctx = TU->getASTContext();
auto InputMatches = match(IM, Ctx);
ASSERT_EQ(1u, InputMatches.size());
Expand Down