Skip to content

Commit 189f3c5

Browse files
committed
Merge from 'main' to 'sycl-web' (15 commits)
2 parents 44d9e6a + f886f7e commit 189f3c5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+15219
-3848
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//===--- SARIFDiagnostic.h - SARIF Diagnostic Formatting -----*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This is a utility class that provides support for constructing a SARIF object
10+
// containing diagnostics.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_CLANG_FRONTEND_SARIFDIAGNOSTIC_H
15+
#define LLVM_CLANG_FRONTEND_SARIFDIAGNOSTIC_H
16+
17+
#include "clang/Basic/Sarif.h"
18+
#include "clang/Frontend/DiagnosticRenderer.h"
19+
#include "llvm/ADT/StringRef.h"
20+
21+
namespace clang {
22+
23+
class SARIFDiagnostic : public DiagnosticRenderer {
24+
public:
25+
SARIFDiagnostic(raw_ostream &OS, const LangOptions &LangOpts,
26+
DiagnosticOptions *DiagOpts, SarifDocumentWriter *Writer);
27+
28+
~SARIFDiagnostic() = default;
29+
30+
SARIFDiagnostic &operator=(const SARIFDiagnostic &&) = delete;
31+
SARIFDiagnostic(SARIFDiagnostic &&) = delete;
32+
SARIFDiagnostic &operator=(const SARIFDiagnostic &) = delete;
33+
SARIFDiagnostic(const SARIFDiagnostic &) = delete;
34+
35+
protected:
36+
void emitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc,
37+
DiagnosticsEngine::Level Level, StringRef Message,
38+
ArrayRef<CharSourceRange> Ranges,
39+
DiagOrStoredDiag D) override;
40+
41+
void emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
42+
DiagnosticsEngine::Level Level,
43+
ArrayRef<CharSourceRange> Ranges) override;
44+
45+
void emitCodeContext(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
46+
SmallVectorImpl<CharSourceRange> &Ranges,
47+
ArrayRef<FixItHint> Hints) override {}
48+
49+
void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) override;
50+
51+
void emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc,
52+
StringRef ModuleName) override;
53+
54+
void emitBuildingModuleLocation(FullSourceLoc Loc, PresumedLoc PLoc,
55+
StringRef ModuleName) override;
56+
57+
private:
58+
raw_ostream &OS;
59+
60+
// Shared between SARIFDiagnosticPrinter and this renderer.
61+
SarifDocumentWriter *Writer;
62+
63+
SarifResult addLocationToResult(SarifResult Result, FullSourceLoc Loc,
64+
PresumedLoc PLoc,
65+
ArrayRef<CharSourceRange> Ranges,
66+
const Diagnostic &Diag);
67+
68+
SarifRule addDiagnosticLevelToRule(SarifRule Rule,
69+
DiagnosticsEngine::Level Level);
70+
71+
llvm::StringRef emitFilename(StringRef Filename, const SourceManager &SM);
72+
};
73+
74+
} // end namespace clang
75+
76+
#endif
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//===-- SARIFDiagnosticPrinter.h - SARIF Diagnostic Client -------*- C++-*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This is a concrete diagnostic client, which prints the diagnostics to
10+
// standard error in SARIF format.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_CLANG_FRONTEND_SARIFDIAGNOSTICPRINTER_H
15+
#define LLVM_CLANG_FRONTEND_SARIFDIAGNOSTICPRINTER_H
16+
17+
#include "clang/Basic/Diagnostic.h"
18+
#include "clang/Basic/LLVM.h"
19+
#include "clang/Basic/Sarif.h"
20+
#include "llvm/ADT/IntrusiveRefCntPtr.h"
21+
#include "llvm/ADT/StringRef.h"
22+
#include <memory>
23+
24+
namespace clang {
25+
class DiagnosticOptions;
26+
class LangOptions;
27+
class SARIFDiagnostic;
28+
class SarifDocumentWriter;
29+
30+
class SARIFDiagnosticPrinter : public DiagnosticConsumer {
31+
public:
32+
SARIFDiagnosticPrinter(raw_ostream &OS, DiagnosticOptions *Diags);
33+
~SARIFDiagnosticPrinter() = default;
34+
35+
SARIFDiagnosticPrinter &operator=(const SARIFDiagnosticPrinter &&) = delete;
36+
SARIFDiagnosticPrinter(SARIFDiagnosticPrinter &&) = delete;
37+
SARIFDiagnosticPrinter &operator=(const SARIFDiagnosticPrinter &) = delete;
38+
SARIFDiagnosticPrinter(const SARIFDiagnosticPrinter &) = delete;
39+
40+
/// setPrefix - Set the diagnostic printer prefix string, which will be
41+
/// printed at the start of any diagnostics. If empty, no prefix string is
42+
/// used.
43+
void setPrefix(llvm::StringRef Value) { Prefix = Value; }
44+
45+
bool hasSarifWriter() const { return Writer != nullptr; }
46+
47+
SarifDocumentWriter &getSarifWriter() const {
48+
assert(Writer && "SarifWriter not set!");
49+
return *Writer;
50+
}
51+
52+
void setSarifWriter(std::unique_ptr<SarifDocumentWriter> SarifWriter) {
53+
Writer = std::move(SarifWriter);
54+
}
55+
56+
void BeginSourceFile(const LangOptions &LO, const Preprocessor *PP) override;
57+
void EndSourceFile() override;
58+
void HandleDiagnostic(DiagnosticsEngine::Level Level,
59+
const Diagnostic &Info) override;
60+
61+
private:
62+
raw_ostream &OS;
63+
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
64+
65+
/// Handle to the currently active SARIF diagnostic emitter.
66+
std::unique_ptr<SARIFDiagnostic> SARIFDiag;
67+
68+
/// A string to prefix to error messages.
69+
std::string Prefix;
70+
71+
std::unique_ptr<SarifDocumentWriter> Writer;
72+
};
73+
74+
} // end namespace clang
75+
76+
#endif

clang/lib/Frontend/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ add_clang_library(clangFrontend
3131
MultiplexConsumer.cpp
3232
PrecompiledPreamble.cpp
3333
PrintPreprocessedOutput.cpp
34+
SARIFDiagnostic.cpp
35+
SARIFDiagnosticPrinter.cpp
3436
SerializedDiagnosticPrinter.cpp
3537
SerializedDiagnosticReader.cpp
3638
TestModuleFileExtension.cpp

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "clang/AST/Decl.h"
1313
#include "clang/Basic/CharInfo.h"
1414
#include "clang/Basic/Diagnostic.h"
15+
#include "clang/Basic/DiagnosticOptions.h"
1516
#include "clang/Basic/FileManager.h"
1617
#include "clang/Basic/LangStandard.h"
1718
#include "clang/Basic/SourceManager.h"
@@ -25,6 +26,7 @@
2526
#include "clang/Frontend/FrontendDiagnostic.h"
2627
#include "clang/Frontend/FrontendPluginRegistry.h"
2728
#include "clang/Frontend/LogDiagnosticPrinter.h"
29+
#include "clang/Frontend/SARIFDiagnosticPrinter.h"
2830
#include "clang/Frontend/SerializedDiagnosticPrinter.h"
2931
#include "clang/Frontend/TextDiagnosticPrinter.h"
3032
#include "clang/Frontend/Utils.h"
@@ -349,6 +351,8 @@ CompilerInstance::createDiagnostics(DiagnosticOptions *Opts,
349351
// implementing -verify.
350352
if (Client) {
351353
Diags->setClient(Client, ShouldOwnClient);
354+
} else if (Opts->getFormat() == DiagnosticOptions::SARIF) {
355+
Diags->setClient(new SARIFDiagnosticPrinter(llvm::errs(), Opts));
352356
} else
353357
Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts));
354358

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
#include "clang/AST/ASTContext.h"
1212
#include "clang/AST/DeclGroup.h"
1313
#include "clang/Basic/Builtins.h"
14+
#include "clang/Basic/DiagnosticOptions.h"
1415
#include "clang/Basic/LangStandard.h"
16+
#include "clang/Basic/Sarif.h"
1517
#include "clang/Frontend/ASTUnit.h"
1618
#include "clang/Frontend/CompilerInstance.h"
1719
#include "clang/Frontend/FrontendDiagnostic.h"
1820
#include "clang/Frontend/FrontendPluginRegistry.h"
1921
#include "clang/Frontend/LayoutOverrideSource.h"
2022
#include "clang/Frontend/MultiplexConsumer.h"
23+
#include "clang/Frontend/SARIFDiagnosticPrinter.h"
2124
#include "clang/Frontend/Utils.h"
2225
#include "clang/Lex/HeaderSearch.h"
2326
#include "clang/Lex/LiteralSupport.h"
@@ -35,6 +38,7 @@
3538
#include "llvm/Support/Path.h"
3639
#include "llvm/Support/Timer.h"
3740
#include "llvm/Support/raw_ostream.h"
41+
#include <memory>
3842
#include <system_error>
3943
using namespace clang;
4044

@@ -717,8 +721,14 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
717721
return false;
718722
}
719723
}
720-
if (!CI.hasSourceManager())
724+
if (!CI.hasSourceManager()) {
721725
CI.createSourceManager(CI.getFileManager());
726+
if (CI.getDiagnosticOpts().getFormat() == DiagnosticOptions::SARIF) {
727+
static_cast<SARIFDiagnosticPrinter *>(&CI.getDiagnosticClient())
728+
->setSarifWriter(
729+
std::make_unique<SarifDocumentWriter>(CI.getSourceManager()));
730+
}
731+
}
722732

723733
// Set up embedding for any specified files. Do this before we load any
724734
// source files, including the primary module map for the compilation.

0 commit comments

Comments
 (0)