xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/tools/clang_generator/diagnostics.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "sandboxed_api/tools/clang_generator/diagnostics.h"
16 
17 #include <cstdint>
18 #include <cstring>
19 #include <utility>
20 
21 #include "absl/status/status.h"
22 #include "absl/strings/cord.h"
23 #include "absl/strings/string_view.h"
24 #include "absl/types/optional.h"
25 #include "clang/Basic/Diagnostic.h"
26 
27 namespace sapi {
28 
29 constexpr absl::string_view kSapiStatusPayload =
30     "https://github.com/google/sandboxed-api";
31 
MakeStatusWithDiagnostic(clang::SourceLocation loc,absl::StatusCode code,absl::string_view message)32 absl::Status MakeStatusWithDiagnostic(clang::SourceLocation loc,
33                                       absl::StatusCode code,
34                                       absl::string_view message) {
35   absl::Status status(code, message);
36   absl::Cord payload;
37   uint64_t raw_loc = loc.getRawEncoding();
38   payload.Append(
39       absl::string_view(reinterpret_cast<char*>(&raw_loc), sizeof(raw_loc)));
40   status.SetPayload(kSapiStatusPayload, std::move(payload));
41   return status;
42 }
43 
MakeStatusWithDiagnostic(clang::SourceLocation loc,absl::string_view message)44 absl::Status MakeStatusWithDiagnostic(clang::SourceLocation loc,
45                                       absl::string_view message) {
46   return MakeStatusWithDiagnostic(loc, absl::StatusCode::kUnknown, message);
47 }
48 
GetDiagnosticLocationFromStatus(const absl::Status & status)49 absl::optional<clang::SourceLocation> GetDiagnosticLocationFromStatus(
50     const absl::Status& status) {
51   if (auto payload =
52           status.GetPayload(kSapiStatusPayload).value_or(absl::Cord());
53       payload.size() == sizeof(uint64_t)) {
54     uint64_t raw_encoding = 0;
55     memcpy(&raw_encoding, payload.Flatten().data(), sizeof(raw_encoding));
56     return clang::SourceLocation::getFromRawEncoding(raw_encoding);
57   }
58   return absl::nullopt;
59 }
60 
61 namespace {
62 
GetDiagnosticBuilder(clang::DiagnosticsEngine & de,clang::SourceLocation loc,clang::DiagnosticsEngine::Level level,absl::string_view message)63 clang::DiagnosticBuilder GetDiagnosticBuilder(
64     clang::DiagnosticsEngine& de, clang::SourceLocation loc,
65     clang::DiagnosticsEngine::Level level, absl::string_view message) {
66   clang::DiagnosticBuilder builder =
67       de.Report(loc, de.getCustomDiagID(level, "header generation: %0"));
68   builder.AddString(llvm::StringRef(message.data(), message.size()));
69   return builder;
70 }
71 
72 }  // namespace
73 
ReportFatalError(clang::DiagnosticsEngine & de,clang::SourceLocation loc,absl::string_view message)74 clang::DiagnosticBuilder ReportFatalError(clang::DiagnosticsEngine& de,
75                                           clang::SourceLocation loc,
76                                           absl::string_view message) {
77   return GetDiagnosticBuilder(de, loc, clang::DiagnosticsEngine::Fatal,
78                               message);
79 }
80 
ReportWarning(clang::DiagnosticsEngine & de,clang::SourceLocation loc,absl::string_view message)81 clang::DiagnosticBuilder ReportWarning(clang::DiagnosticsEngine& de,
82                                        clang::SourceLocation loc,
83                                        absl::string_view message) {
84   return GetDiagnosticBuilder(de, loc, clang::DiagnosticsEngine::Warning,
85                               message);
86 }
87 
88 }  // namespace sapi
89