• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "clang/Basic/SourceLocation.h"
27 #include "llvm/ADT/StringRef.h"
28 
29 namespace sapi {
30 
31 constexpr absl::string_view kSapiStatusPayload =
32     "https://github.com/google/sandboxed-api";
33 
MakeStatusWithDiagnostic(clang::SourceLocation loc,absl::StatusCode code,absl::string_view message)34 absl::Status MakeStatusWithDiagnostic(clang::SourceLocation loc,
35                                       absl::StatusCode code,
36                                       absl::string_view message) {
37   absl::Status status(code, message);
38   absl::Cord payload;
39   uint64_t raw_loc = loc.getRawEncoding();
40   payload.Append(
41       absl::string_view(reinterpret_cast<char*>(&raw_loc), sizeof(raw_loc)));
42   status.SetPayload(kSapiStatusPayload, std::move(payload));
43   return status;
44 }
45 
MakeStatusWithDiagnostic(clang::SourceLocation loc,absl::string_view message)46 absl::Status MakeStatusWithDiagnostic(clang::SourceLocation loc,
47                                       absl::string_view message) {
48   return MakeStatusWithDiagnostic(loc, absl::StatusCode::kUnknown, message);
49 }
50 
GetDiagnosticLocationFromStatus(const absl::Status & status)51 absl::optional<clang::SourceLocation> GetDiagnosticLocationFromStatus(
52     const absl::Status& status) {
53   if (auto payload =
54           status.GetPayload(kSapiStatusPayload).value_or(absl::Cord());
55       payload.size() == sizeof(uint64_t)) {
56     uint64_t raw_encoding = 0;
57     memcpy(&raw_encoding, payload.Flatten().data(), sizeof(raw_encoding));
58     return clang::SourceLocation::getFromRawEncoding(raw_encoding);
59   }
60   return absl::nullopt;
61 }
62 
63 namespace {
64 
GetDiagnosticBuilder(clang::DiagnosticsEngine & de,clang::SourceLocation loc,clang::DiagnosticsEngine::Level level,absl::string_view message)65 clang::DiagnosticBuilder GetDiagnosticBuilder(
66     clang::DiagnosticsEngine& de, clang::SourceLocation loc,
67     clang::DiagnosticsEngine::Level level, absl::string_view message) {
68   clang::DiagnosticBuilder builder =
69       de.Report(loc, de.getCustomDiagID(level, "header generation: %0"));
70   builder.AddString(llvm::StringRef(message.data(), message.size()));
71   return builder;
72 }
73 
74 }  // namespace
75 
ReportFatalError(clang::DiagnosticsEngine & de,clang::SourceLocation loc,absl::string_view message)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 
ReportWarning(clang::DiagnosticsEngine & de,clang::SourceLocation loc,absl::string_view message)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 
90 }  // namespace sapi
91