Skip to content

Commit d4ceb23

Browse files
committed
code review
1 parent b1a30c8 commit d4ceb23

File tree

5 files changed

+33
-24
lines changed

5 files changed

+33
-24
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,9 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
239239
ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster(),
240240
const FileContentMappings &VirtualMappedFiles = FileContentMappings(),
241241
DiagnosticConsumer *DiagConsumer = nullptr,
242-
CaptureDiagsKind CaptureKind = CaptureDiagsKind::None,
243242
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS =
244-
llvm::vfs::getRealFileSystem());
243+
llvm::vfs::getRealFileSystem(),
244+
CaptureDiagsKind CaptureKind = CaptureDiagsKind::None);
245245

246246
/// Utility to run a FrontendAction in a single clang invocation.
247247
class ToolInvocation {

clang/lib/Tooling/Tooling.cpp

Lines changed: 9 additions & 7 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 CaptureKinds;
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 CaptureDiagnosticKinds = CaptureDiagsKind::None)
653+
: ASTs(ASTs), CaptureKinds(CaptureDiagnosticKinds) {}
650654

651655
bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
652656
FileManager *Files,
@@ -665,8 +669,6 @@ class ASTBuilderAction : public ToolAction {
665669
ASTs.push_back(std::move(AST));
666670
return true;
667671
}
668-
669-
CaptureDiagsKind CaptureKinds{CaptureDiagsKind::None};
670672
};
671673

672674
} // namespace
@@ -694,12 +696,12 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
694696
StringRef Code, const std::vector<std::string> &Args, StringRef FileName,
695697
StringRef ToolName, std::shared_ptr<PCHContainerOperations> PCHContainerOps,
696698
ArgumentsAdjuster Adjuster, const FileContentMappings &VirtualMappedFiles,
697-
DiagnosticConsumer *DiagConsumer, CaptureDiagsKind CaptureKind,
698-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS) {
699+
DiagnosticConsumer *DiagConsumer,
700+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
701+
CaptureDiagsKind CaptureKind) {
699702
std::vector<std::unique_ptr<ASTUnit>> ASTs;
700703

701-
ASTBuilderAction Action(ASTs);
702-
Action.CaptureKinds = CaptureDiagsKind::All;
704+
ASTBuilderAction Action(ASTs, CaptureKind);
703705

704706
auto OverlayFileSystem =
705707
llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(

clang/unittests/Sema/HeuristicResolverTest.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ template <typename InputNode>
3333
using ResolveFnT = std::function<std::vector<const NamedDecl *>(
3434
const HeuristicResolver *, InputNode)>;
3535

36-
std::string format_error(const clang::StoredDiagnostic *D) {
36+
std::string format_error(const clang::StoredDiagnostic &D) {
3737
std::ostringstream Msg{};
38-
if (D->getLevel() == DiagnosticsEngine::Level::Ignored)
38+
if (D.getLevel() == DiagnosticsEngine::Level::Ignored)
3939
Msg << "Ignored: ";
40-
if (D->getLevel() == DiagnosticsEngine::Level::Note)
40+
if (D.getLevel() == DiagnosticsEngine::Level::Note)
4141
Msg << "Note: ";
42-
if (D->getLevel() == DiagnosticsEngine::Level::Remark)
42+
if (D.getLevel() == DiagnosticsEngine::Level::Remark)
4343
Msg << "Remark: ";
44-
if (D->getLevel() == DiagnosticsEngine::Level::Warning)
44+
if (D.getLevel() == DiagnosticsEngine::Level::Warning)
4545
Msg << "Warning: ";
46-
if (D->getLevel() == DiagnosticsEngine::Level::Error)
46+
if (D.getLevel() == DiagnosticsEngine::Level::Error)
4747
Msg << "Error: ";
48-
if (D->getLevel() == DiagnosticsEngine::Level::Fatal)
48+
if (D.getLevel() == DiagnosticsEngine::Level::Fatal)
4949
Msg << "Fatal: ";
50-
Msg << D->getID() << ": " << D->getMessage().str();
50+
Msg << D.getID() << ": " << D.getMessage().str();
5151
return Msg.str();
5252
}
5353

@@ -61,14 +61,10 @@ template <typename InputNode, typename ParamT, typename InputMatcher,
6161
typename... OutputMatchers>
6262
void expectResolution(llvm::StringRef Code, ResolveFnT<ParamT> ResolveFn,
6363
const InputMatcher &IM, const OutputMatchers &...OMS) {
64-
llvm::SmallSet<unsigned int, 16> IgnoredDiagnostics{};
6564
auto TU = tooling::buildASTFromCodeWithArgs(Code, {"-std=c++23"});
6665

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);
66+
for (const auto &D : TU->storedDiagnostics()) {
67+
EXPECT_TRUE(D.getLevel() < DiagnosticsEngine::Error) << format_error(D);
7268
}
7369

7470
auto &Ctx = TU->getASTContext();

clang/unittests/Tooling/ToolingTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ TEST(buildASTFromCode, FileSystem) {
161161
R"(#include "included_file.h")", {}, "input.cc", "clang-tool",
162162
std::make_shared<PCHContainerOperations>(),
163163
getClangStripDependencyFileAdjuster(), FileContentMappings(), nullptr,
164-
CaptureDiagsKind::None, InMemoryFileSystem);
164+
InMemoryFileSystem);
165165
ASSERT_TRUE(AST.get());
166166
EXPECT_TRUE(FindClassDeclX(AST.get()));
167167
}

0 commit comments

Comments
 (0)