Skip to content

Commit 719f1ca

Browse files
dvyukovcopybara-github
authored andcommitted
Make it possible to test error messages produces by the clang tool
PiperOrigin-RevId: 809878622 Change-Id: Iecb0367bef06aee7525c8b8d6a2989fb1c3a6c22
1 parent ff53a96 commit 719f1ca

4 files changed

Lines changed: 38 additions & 37 deletions

File tree

sandboxed_api/bazel/sapi.bzl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,3 +688,26 @@ _sandboxed_library_gen = rule(
688688
},
689689
toolchains = use_cpp_toolchain(),
690690
)
691+
692+
def _sandboxed_library_gen_errors_impl(ctx):
693+
gen_action = [a for a in ctx.attr.target.actions if a.mnemonic == "SandboxedLibraryGen"][0]
694+
ctx.actions.run_shell(
695+
inputs = gen_action.inputs,
696+
outputs = [ctx.outputs.errors],
697+
arguments = gen_action.args,
698+
# Replace bazel hashes with something stable.
699+
command = "$@ 2>&1 | sed 's#[[:xdigit:]]\\{32,\\}#hexhexhexhexhexhexhexhexhexhexhex#g' > " + ctx.outputs.errors.path,
700+
mnemonic = "SandboxedLibraryGenErrors",
701+
)
702+
return [DefaultInfo(files = depset([ctx.outputs.errors]))]
703+
704+
# Helper target for tests that saves the errors generated by the clang generator
705+
# into the specified errors file. This allows to test errors messages produced
706+
# by the generator.
707+
sandboxed_library_gen_errors = rule(
708+
implementation = _sandboxed_library_gen_errors_impl,
709+
attrs = {
710+
"target": attr.label(),
711+
"errors": attr.output(),
712+
},
713+
)

sandboxed_api/tools/clang_generator/diagnostics.cc

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -60,31 +60,14 @@ absl::optional<clang::SourceLocation> GetDiagnosticLocationFromStatus(
6060
return absl::nullopt;
6161
}
6262

63-
namespace {
64-
65-
clang::DiagnosticBuilder GetDiagnosticBuilder(
66-
clang::DiagnosticsEngine& de, clang::SourceLocation loc,
67-
clang::DiagnosticsEngine::Level level, absl::string_view message) {
63+
clang::DiagnosticBuilder Report(clang::DiagnosticsEngine& de,
64+
clang::SourceLocation loc,
65+
clang::DiagnosticsEngine::Level level,
66+
absl::string_view message) {
6867
clang::DiagnosticBuilder builder =
6968
de.Report(loc, de.getCustomDiagID(level, "header generation: %0"));
7069
builder.AddString(llvm::StringRef(message.data(), message.size()));
7170
return builder;
7271
}
7372

74-
} // namespace
75-
76-
clang::DiagnosticBuilder ReportFatalError(clang::DiagnosticsEngine& de,
77-
clang::SourceLocation loc,
78-
absl::string_view message) {
79-
return GetDiagnosticBuilder(de, loc, clang::DiagnosticsEngine::Fatal,
80-
message);
81-
}
82-
83-
clang::DiagnosticBuilder ReportWarning(clang::DiagnosticsEngine& de,
84-
clang::SourceLocation loc,
85-
absl::string_view message) {
86-
return GetDiagnosticBuilder(de, loc, clang::DiagnosticsEngine::Warning,
87-
message);
88-
}
89-
9073
} // namespace sapi

sandboxed_api/tools/clang_generator/diagnostics.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,10 @@ absl::Status MakeStatusWithDiagnostic(clang::SourceLocation loc,
3838
absl::optional<clang::SourceLocation> GetDiagnosticLocationFromStatus(
3939
const absl::Status& status);
4040

41-
clang::DiagnosticBuilder ReportWarning(clang::DiagnosticsEngine& de,
42-
clang::SourceLocation loc,
43-
absl::string_view message);
44-
45-
clang::DiagnosticBuilder ReportFatalError(clang::DiagnosticsEngine& de,
46-
clang::SourceLocation loc,
47-
absl::string_view message);
41+
clang::DiagnosticBuilder Report(clang::DiagnosticsEngine& de,
42+
clang::SourceLocation loc,
43+
clang::DiagnosticsEngine::Level level,
44+
absl::string_view message);
4845

4946
} // namespace sapi
5047

sandboxed_api/tools/clang_generator/generator.cc

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ bool GeneratorASTVisitor::VisitFunctionDecl(clang::FunctionDecl* decl) {
201201

202202
void GeneratorASTConsumer::HandleTranslationUnit(clang::ASTContext& context) {
203203
if (!visitor_.TraverseDecl(context.getTranslationUnitDecl())) {
204-
ReportFatalError(context.getDiagnostics(),
205-
context.getTranslationUnitDecl()->getBeginLoc(),
206-
"AST traversal exited early.");
204+
Report(context.getDiagnostics(),
205+
context.getTranslationUnitDecl()->getBeginLoc(),
206+
clang::DiagnosticsEngine::Fatal, "AST traversal exited early.");
207207
return;
208208
}
209209

@@ -220,12 +220,10 @@ void GeneratorASTConsumer::HandleTranslationUnit(clang::ASTContext& context) {
220220
if (!status.ok()) {
221221
clang::SourceLocation loc =
222222
GetDiagnosticLocationFromStatus(status).value_or(func->getBeginLoc());
223-
if (absl::IsCancelled(status)) {
224-
ReportWarning(context.getDiagnostics(), loc, status.message());
225-
continue;
226-
}
227-
ReportFatalError(context.getDiagnostics(), loc, status.message());
228-
break;
223+
clang::DiagnosticsEngine::Level level =
224+
absl::IsCancelled(status) ? clang::DiagnosticsEngine::Warning
225+
: clang::DiagnosticsEngine::Error;
226+
Report(context.getDiagnostics(), loc, level, status.message());
229227
}
230228
}
231229
}

0 commit comments

Comments
 (0)