cjdb updated this revision to Diff 502190.
cjdb added a comment.

fixes string goof


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D145201/new/

https://reviews.llvm.org/D145201

Files:
  clang/include/clang/Frontend/SARIFDiagnostic.h
  clang/lib/Basic/Sarif.cpp
  clang/lib/Frontend/SARIFDiagnostic.cpp
  clang/test/Frontend/sarif-diagnostics.cpp
  clang/test/Frontend/sarif-diagnostics.hpp

Index: clang/test/Frontend/sarif-diagnostics.hpp
===================================================================
--- /dev/null
+++ clang/test/Frontend/sarif-diagnostics.hpp
@@ -0,0 +1 @@
+Test test;
Index: clang/test/Frontend/sarif-diagnostics.cpp
===================================================================
--- clang/test/Frontend/sarif-diagnostics.cpp
+++ clang/test/Frontend/sarif-diagnostics.cpp
@@ -27,6 +27,8 @@
     x + y;
 }
 
+#include "sarif-diagnostics.hpp"
+
 // RUN: %clang -fsyntax-only -Wall -Wextra -fdiagnostics-format=sarif %s > %t.txt 2>&1 || true
 // RUN: FileCheck -dump-input=always %s --input-file=%t.txt --check-prefixes=STDERR,SARIF
 
@@ -46,6 +48,17 @@
 // SARIF:           "roles":[
 // SARIF:             "resultFile"
 // SARIF:           ]
+// SARIF:         },
+// SARIF:         {
+// SARIF:           "length":{{[0-9]+}},
+// SARIF:           "location":{
+// SARIF:             "index":1,
+// SARIF:             "uri":"file://{{[^"]+test/Frontend/sarif-diagnostics.hpp}}"
+// SARIF:           },
+// SARIF:           "mimeType":"text/plain",
+// SARIF:           "roles":[
+// SARIF:             "resultFile"
+// SARIF:           ]
 // SARIF:         }
 // SARIF:       ],
 // SARIF:       "columnKind":"unicodeCodePoints",
@@ -172,7 +185,7 @@
 // SARIF:               "physicalLocation":{
 // SARIF:                 "artifactLocation":{
 // SARIF:                   "index":0,
-// SARIF:                   "uri":{{"file://[^"]+/clang/test/Frontend/sarif-diagnostics.cpp"}}
+// SARIF:                   "uri":{{"file://[^"]+clang/test/Frontend/sarif-diagnostics.cpp"}}
 // SARIF:                 },
 // SARIF:                 "region":{
 // SARIF:                   "endColumn":10,
@@ -195,7 +208,7 @@
 // SARIF:               "physicalLocation":{
 // SARIF:                 "artifactLocation":{
 // SARIF:                   "index":0,
-// SARIF:                   "uri":{{"file://[^"]+/clang/test/Frontend/sarif-diagnostics.cpp"}}
+// SARIF:                   "uri":{{"file://[^"]+clang/test/Frontend/sarif-diagnostics.cpp"}}
 // SARIF:                 },
 // SARIF:                 "region":{
 // SARIF:                   "endColumn":12,
@@ -284,6 +297,52 @@
 // SARIF:           },
 // SARIF:           "ruleId":"4567",
 // SARIF:           "ruleIndex":8
+// SARIF:         },
+// SARIF:         {
+// SARIF:           "level":"note",
+// SARIF:           "locations":[
+// SARIF:             {
+// SARIF:               "physicalLocation":{
+// SARIF:                 "artifactLocation":{
+// SARIF:                   "index":0,
+// SARIF:                   "uri":{{"file://[^"]+/clang/test/Frontend/sarif-diagnostics.cpp"}}
+// SARIF:                 },
+// SARIF:                 "region":{
+// SARIF:                   "endColumn":10,
+// SARIF:                   "startColumn":10,
+// SARIF:                   "startLine":30
+// SARIF:                 }
+// SARIF:               }
+// SARIF:             }
+// SARIF:           ],
+// SARIF:           "message":{
+// SARIF:             "text":"in file included from {{[^"]+test/Frontend/sarif-diagnostics.cpp:30:}}\n"
+// SARIF:           },
+// SARIF:           "ruleId":"-1",
+// SARIF:           "ruleIndex":9
+// SARIF:         },
+// SARIF:         {
+// SARIF:           "level":"error",
+// SARIF:           "locations":[
+// SARIF:             {
+// SARIF:               "physicalLocation":{
+// SARIF:                 "artifactLocation":{
+// SARIF:                   "index":1,
+// SARIF:                   "uri":"file:///{{[^"]+/test/Frontend/sarif-diagnostics.hpp}}"
+// SARIF:                 },
+// SARIF:                 "region":{
+// SARIF:                   "endColumn":1,
+// SARIF:                   "startColumn":1,
+// SARIF:                   "startLine":1
+// SARIF:                 }
+// SARIF:               }
+// SARIF:             }
+// SARIF:           ],
+// SARIF:           "message":{
+// SARIF:             "text":"unknown type name 'Test'"
+// SARIF:           },
+// SARIF:           "ruleId":"4657",
+// SARIF:           "ruleIndex":10
 // SARIF:         }
 // SARIF:       ],
 // SARIF:       "tool":{
@@ -400,6 +459,30 @@
 // SARIF:               },
 // SARIF:               "id":"4567",
 // SARIF:               "name":""
+// SARIF:             },
+// SARIF:             {
+// SARIF:               "defaultConfiguration":{
+// SARIF:                 "enabled":true,
+// SARIF:                 "level":"note",
+// SARIF:                 "rank":-1
+// SARIF:               },
+// SARIF:               "fullDescription":{
+// SARIF:                 "text":""
+// SARIF:               },
+// SARIF:               "id":"-1",
+// SARIF:               "name":""
+// SARIF:             },
+// SARIF:             {
+// SARIF:               "defaultConfiguration":{
+// SARIF:                 "enabled":true,
+// SARIF:                 "level":"error",
+// SARIF:                 "rank":50
+// SARIF:               },
+// SARIF:               "fullDescription":{
+// SARIF:                 "text":""
+// SARIF:               },
+// SARIF:               "id":"4657",
+// SARIF:               "name":""
 // SARIF:             }
 // SARIF:           ],
 // SARIF:           "version":"17.0.0"
@@ -409,4 +492,4 @@
 // SARIF:   ],
 // SARIF:   "version":"2.1.0"
 // SARIF: }
-// STDERR: 2 warnings and 6 errors generated.
+// STDERR: 2 warnings and 7 errors generated.
Index: clang/lib/Frontend/SARIFDiagnostic.cpp
===================================================================
--- clang/lib/Frontend/SARIFDiagnostic.cpp
+++ clang/lib/Frontend/SARIFDiagnostic.cpp
@@ -26,6 +26,7 @@
 #include "llvm/Support/Path.h"
 #include "llvm/Support/raw_ostream.h"
 #include <algorithm>
+#include <sstream>
 #include <string>
 
 namespace clang {
@@ -56,14 +57,15 @@
       SarifResult::create(RuleIdx).setDiagnosticMessage(Message);
 
   if (Loc.isValid())
-    Result = addLocationToResult(Result, Loc, PLoc, Ranges, *Diag);
+    Result = addLocationToResult(Result, Loc, PLoc, Ranges);
 
   Writer->appendResult(Result);
 }
 
-SarifResult SARIFDiagnostic::addLocationToResult(
-    SarifResult Result, FullSourceLoc Loc, PresumedLoc PLoc,
-    ArrayRef<CharSourceRange> Ranges, const Diagnostic &Diag) {
+SarifResult
+SARIFDiagnostic::addLocationToResult(SarifResult Result, FullSourceLoc Loc,
+                                     PresumedLoc PLoc,
+                                     ArrayRef<CharSourceRange> Ranges) {
   SmallVector<CharSourceRange> Locations = {};
 
   if (PLoc.isInvalid()) {
@@ -209,7 +211,22 @@
 }
 
 void SARIFDiagnostic::emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) {
-  assert(false && "Not implemented in SARIF mode");
+  SarifRule Rule = SarifRule::create().setRuleId("-1");
+  Rule = addDiagnosticLevelToRule(Rule, DiagnosticsEngine::Level::Note);
+
+  unsigned RuleIdx = Writer->createRule(Rule);
+
+  SmallString<200> MessageStorage;
+  llvm::raw_svector_ostream Message(MessageStorage);
+  Message << "in file included from " << PLoc.getFilename() << ':'
+          << PLoc.getLine() << ":\n";
+  SarifResult Result =
+      SarifResult::create(RuleIdx).setDiagnosticMessage(Message.str());
+
+  if (Loc.isValid())
+    Result = addLocationToResult(Result, Loc, PLoc, {});
+
+  Writer->appendResult(Result);
 }
 
 void SARIFDiagnostic::emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc,
Index: clang/lib/Basic/Sarif.cpp
===================================================================
--- clang/lib/Basic/Sarif.cpp
+++ clang/lib/Basic/Sarif.cpp
@@ -311,6 +311,10 @@
       Artifact["offset"] = *A.Offset;
     Artifacts->push_back(json::Value(std::move(Artifact)));
   }
+  llvm::sort(*Artifacts, [](const json::Value &x, const json::Value &y) {
+    return x.getAsObject()->getNumber("index") <
+           y.getAsObject()->getNumber("index");
+  });
 
   // Clear, reset temporaries before next run.
   reset();
Index: clang/include/clang/Frontend/SARIFDiagnostic.h
===================================================================
--- clang/include/clang/Frontend/SARIFDiagnostic.h
+++ clang/include/clang/Frontend/SARIFDiagnostic.h
@@ -60,8 +60,7 @@
 
   SarifResult addLocationToResult(SarifResult Result, FullSourceLoc Loc,
                                   PresumedLoc PLoc,
-                                  ArrayRef<CharSourceRange> Ranges,
-                                  const Diagnostic &Diag);
+                                  ArrayRef<CharSourceRange> Ranges);
 
   SarifRule addDiagnosticLevelToRule(SarifRule Rule,
                                      DiagnosticsEngine::Level Level);
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to