We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4d19eab commit adae95cCopy full SHA for adae95c
checker/BUILD
@@ -27,7 +27,6 @@ cc_library(
27
hdrs = ["type_check_issue.h"],
28
deps = [
29
"//common:source",
30
- "@com_google_absl//absl/strings",
31
"@com_google_absl//absl/strings:str_format",
32
"@com_google_absl//absl/strings:string_view",
33
],
@@ -45,6 +44,7 @@ cc_test(
45
44
46
cc_library(
47
name = "validation_result",
+ srcs = ["validation_result.cc"],
48
hdrs = ["validation_result.h"],
49
50
":type_check_issue",
@@ -53,6 +53,7 @@ cc_library(
53
"@com_google_absl//absl/base:nullability",
54
"@com_google_absl//absl/status",
55
"@com_google_absl//absl/status:statusor",
56
+ "@com_google_absl//absl/strings",
57
"@com_google_absl//absl/types:span",
58
59
)
@@ -64,6 +65,7 @@ cc_test(
64
65
66
":validation_result",
67
"//base/ast_internal:ast_impl",
68
+ "//common:source",
69
"//internal:testing",
70
71
"@com_google_absl//absl/status:status_matchers",
checker/type_check_issue.cc
@@ -16,7 +16,6 @@
16
17
#include <string>
18
19
-#include "absl/strings/str_cat.h"
20
#include "absl/strings/str_format.h"
21
#include "absl/strings/string_view.h"
22
#include "common/source.h"
@@ -42,15 +41,19 @@ absl::string_view SeverityString(TypeCheckIssue::Severity severity) {
42
41
43
} // namespace
-std::string TypeCheckIssue::ToDisplayString(const Source& source) const {
+std::string TypeCheckIssue::ToDisplayString(const Source* source) const {
int column = location_.column;
// convert to 1-based if it's in range.
int display_column = column >= 0 ? column + 1 : column;
- return absl::StrCat(
- absl::StrFormat("%s: %s:%d:%d: %s", SeverityString(severity_),
51
- source.description(), location_.line, display_column,
52
- message_),
- source.DisplayErrorLocation(location_));
+ if (source) {
+ return absl::StrFormat("%s: %s:%d:%d: %s%s", SeverityString(severity_),
+ source->description(), location_.line,
+ display_column, message_,
+ source->DisplayErrorLocation(location_));
+ }
+
+ return absl::StrFormat("%s: :%d:%d: %s", SeverityString(severity_),
+ location_.line, display_column, message_);
}
} // namespace cel
checker/type_check_issue.h
@@ -48,7 +48,11 @@ class TypeCheckIssue {
// Format the issue highlighting the source position.
- std::string ToDisplayString(const Source& source) const;
+ std::string ToDisplayString(const Source* source) const;
+ std::string ToDisplayString(const Source& source) const {
+ return ToDisplayString(&source);
absl::string_view message() const { return message_; }
Severity severity() const { return severity_; }
checker/validation_result.cc
@@ -0,0 +1,32 @@
1
+// Copyright 2025 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 "checker/validation_result.h"
+#include <string>
+#include "absl/strings/str_cat.h"
+#include "absl/strings/str_join.h"
+#include "checker/type_check_issue.h"
23
+namespace cel {
24
25
+std::string ValidationResult::FormatError() const {
26
+ return absl::StrJoin(
+ issues_, "\n", [this](std::string* out, const TypeCheckIssue& issue) {
+ absl::StrAppend(out, issue.ToDisplayString(source_.get()));
+ });
+}
+} // namespace cel
checker/validation_result.h
@@ -16,6 +16,7 @@
#define THIRD_PARTY_CEL_CPP_CHECKER_VALIDATION_RESULT_H_
#include <memory>
#include <utility>
#include <vector>
@@ -68,6 +69,22 @@ class ValidationResult {
return std::move(source_);
72
+ // Returns a string representation of the issues in the result suitable for
73
+ // display.
74
+ //
75
+ // The result is empty if no issues are present.
76
77
+ // The result is formatted similarly to CEL-Java and CEL-Go, but we do not
78
+ // give strong guarantees on the format or stability.
79
80
+ // Example:
81
82
+ // ERROR: <source description>:1:3: Issue1
83
+ // | source.cel
84
+ // | ..^
85
+ // INFORMATION: <source description>:-1:-1: Issue2
86
+ std::string FormatError() const;
87
88
private:
89
absl::Nullable<std::unique_ptr<Ast>> ast_;
90
std::vector<TypeCheckIssue> issues_;
checker/validation_result_test.cc
@@ -15,11 +15,13 @@
#include "checker/validation_result.h"
+#include <utility>
#include "absl/status/status.h"
#include "absl/status/status_matchers.h"
#include "base/ast_internal/ast_impl.h"
#include "checker/type_check_issue.h"
+#include "common/source.h"
#include "internal/testing.h"
namespace cel {
@@ -65,5 +67,24 @@ TEST(ValidationResultTest, GetIssues) {
EXPECT_THAT(result.GetIssues()[1].severity(), Severity::kInformation);
+TEST(ValidationResultTest, FormatError) {
+ ValidationResult result(
+ {TypeCheckIssue::CreateError({1, 2}, "Issue1"),
+ TypeCheckIssue(Severity::kInformation, {-1, -1}, "Issue2")});
+ EXPECT_FALSE(result.IsValid());
+ ASSERT_OK_AND_ASSIGN(std::unique_ptr<Source> source,
+ NewSource("source.cel", "<description>"));
+ result.SetSource(std::move(source));
+ ASSERT_THAT(result.GetIssues(), SizeIs(2));
+ EXPECT_THAT(result.FormatError(),
+ "ERROR: <description>:1:3: Issue1\n"
+ " | source.cel\n"
+ " | ..^\n"
+ "INFORMATION: <description>:-1:-1: Issue2");
0 commit comments