Skip to content

Commit d58d955

Browse files
Merge branch 'main' into issue_169433
2 parents d6a83b3 + bc9f96a commit d58d955

File tree

127 files changed

+1578
-967
lines changed

Some content is hidden

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

127 files changed

+1578
-967
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,10 @@ Improvements to Clang's diagnostics
465465
Objective-C method and block declarations when calling format functions. It is part
466466
of the format-nonliteral diagnostic (#GH60718)
467467

468+
- Fixed a crash when enabling ``-fdiagnostics-format=sarif`` and the output
469+
carries messages like 'In file included from ...' or 'In module ...'.
470+
Now the include/import locations are written into `sarif.run.result.relatedLocations`.
471+
468472
Improvements to Clang's time-trace
469473
----------------------------------
470474

clang/include/clang/Basic/Sarif.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ class SarifResult {
325325
std::string HostedViewerURI;
326326
llvm::SmallDenseMap<StringRef, std::string, 4> PartialFingerprints;
327327
llvm::SmallVector<CharSourceRange, 8> Locations;
328+
llvm::SmallVector<CharSourceRange, 8> RelatedLocations;
328329
llvm::SmallVector<ThreadFlow, 8> ThreadFlows;
329330
std::optional<SarifResultLevel> LevelOverride;
330331

@@ -354,16 +355,29 @@ class SarifResult {
354355
return *this;
355356
}
356357

357-
SarifResult setLocations(llvm::ArrayRef<CharSourceRange> DiagLocs) {
358+
SarifResult addLocations(llvm::ArrayRef<CharSourceRange> DiagLocs) {
358359
#ifndef NDEBUG
359360
for (const auto &Loc : DiagLocs) {
360361
assert(Loc.isCharRange() &&
361362
"SARIF Results require character granular source ranges!");
362363
}
363364
#endif
364-
Locations.assign(DiagLocs.begin(), DiagLocs.end());
365+
Locations.append(DiagLocs.begin(), DiagLocs.end());
365366
return *this;
366367
}
368+
369+
SarifResult addRelatedLocations(llvm::ArrayRef<CharSourceRange> DiagLocs) {
370+
#ifndef NDEBUG
371+
for (const auto &Loc : DiagLocs) {
372+
assert(
373+
Loc.isCharRange() &&
374+
"SARIF RelatedLocations require character granular source ranges!");
375+
}
376+
#endif
377+
RelatedLocations.append(DiagLocs.begin(), DiagLocs.end());
378+
return *this;
379+
}
380+
367381
SarifResult setThreadFlows(llvm::ArrayRef<ThreadFlow> ThreadFlowResults) {
368382
ThreadFlows.assign(ThreadFlowResults.begin(), ThreadFlowResults.end());
369383
return *this;

clang/include/clang/Frontend/SARIFDiagnostic.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,20 @@ class SARIFDiagnostic : public DiagnosticRenderer {
6363
ArrayRef<CharSourceRange> Ranges,
6464
const Diagnostic &Diag);
6565

66+
SarifResult addRelatedLocationToResult(SarifResult Result, FullSourceLoc Loc,
67+
PresumedLoc PLoc);
68+
69+
llvm::SmallVector<CharSourceRange>
70+
getSarifLocation(FullSourceLoc Loc, PresumedLoc PLoc,
71+
ArrayRef<CharSourceRange> Ranges);
72+
6673
SarifRule addDiagnosticLevelToRule(SarifRule Rule,
6774
DiagnosticsEngine::Level Level);
6875

6976
llvm::StringRef emitFilename(StringRef Filename, const SourceManager &SM);
77+
78+
llvm::SmallVector<std::pair<FullSourceLoc, PresumedLoc>>
79+
RelatedLocationsCache;
7080
};
7181

7282
} // end namespace clang

clang/include/clang/Options/Options.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,8 @@ def mno_mpx : Flag<["-"], "mno-mpx">, Group<clang_ignored_legacy_options_Group>;
320320

321321
// Group that ignores all gcc optimizations that won't be implemented
322322
def clang_ignored_gcc_optimization_f_Group : OptionGroup<
323-
"<clang_ignored_gcc_optimization_f_Group>">, Group<f_Group>, Flags<[Ignored]>;
323+
"<clang_ignored_gcc_optimization_f_Group>">,
324+
Group<f_Group>, Flags<[Ignored]>, Visibility<[ClangOption, FlangOption]>;
324325

325326
class DiagnosticOpts<string base>
326327
: KeyPathAndMacro<"DiagnosticOpts->", base, "DIAG_"> {}

clang/lib/Basic/Sarif.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,14 @@ void SarifDocumentWriter::appendResult(const SarifResult &Result) {
404404
Ret["locations"] = std::move(Locs);
405405
}
406406

407+
if (!Result.RelatedLocations.empty()) {
408+
json::Array ReLocs;
409+
for (auto &Range : Result.RelatedLocations) {
410+
ReLocs.emplace_back(createLocation(createPhysicalLocation(Range)));
411+
}
412+
Ret["relatedLocations"] = std::move(ReLocs);
413+
}
414+
407415
if (!Result.PartialFingerprints.empty()) {
408416
json::Object fingerprints = {};
409417
for (auto &pair : Result.PartialFingerprints) {

clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,9 @@ void CGOpenMPRuntimeGPU::emitParallelCall(
12911291
else
12921292
NumThreadsVal = Bld.CreateZExtOrTrunc(NumThreadsVal, CGF.Int32Ty);
12931293

1294+
// No strict prescriptiveness for the number of threads.
1295+
llvm::Value *StrictNumThreadsVal = llvm::ConstantInt::get(CGF.Int32Ty, 0);
1296+
12941297
assert(IfCondVal && "Expected a value");
12951298
llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc);
12961299
llvm::Value *Args[] = {
@@ -1303,9 +1306,11 @@ void CGOpenMPRuntimeGPU::emitParallelCall(
13031306
ID,
13041307
Bld.CreateBitOrPointerCast(CapturedVarsAddrs.emitRawPointer(CGF),
13051308
CGF.VoidPtrPtrTy),
1306-
llvm::ConstantInt::get(CGM.SizeTy, CapturedVars.size())};
1309+
llvm::ConstantInt::get(CGM.SizeTy, CapturedVars.size()),
1310+
StrictNumThreadsVal};
1311+
13071312
CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
1308-
CGM.getModule(), OMPRTL___kmpc_parallel_51),
1313+
CGM.getModule(), OMPRTL___kmpc_parallel_60),
13091314
Args);
13101315
};
13111316

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,14 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
959959
if (const Arg *A = Args.getLastArg(Opt))
960960
D.Diag(diag::warn_drv_invalid_argument_for_flang) << A->getSpelling();
961961

962+
// Warn about options that are ignored by flang. These are options that are
963+
// accepted by gfortran, but have no equivalent in flang.
964+
for (const Arg *A :
965+
Args.filtered(options::OPT_clang_ignored_gcc_optimization_f_Group)) {
966+
D.Diag(diag::warn_ignored_gcc_optimization) << A->getAsString(Args);
967+
A->claim();
968+
}
969+
962970
const InputInfo &Input = Inputs[0];
963971
types::ID InputType = Input.getType();
964972

clang/lib/Frontend/SARIFDiagnostic.cpp

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,48 @@ void SARIFDiagnostic::emitDiagnosticMessage(
5858
if (Loc.isValid())
5959
Result = addLocationToResult(Result, Loc, PLoc, Ranges, *Diag);
6060

61+
for (auto &[RelLoc, RelPLoc] : RelatedLocationsCache)
62+
Result = addRelatedLocationToResult(Result, RelLoc, RelPLoc);
63+
RelatedLocationsCache.clear();
64+
6165
Writer->appendResult(Result);
6266
}
6367

68+
void SARIFDiagnostic::emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) {
69+
// We always emit include location before results, for example:
70+
//
71+
// In file included from ...
72+
// In file included from ...
73+
// error: ...
74+
//
75+
// At this time We cannot peek the SarifRule. But what we
76+
// do is to push it into a cache and wait for next time
77+
// \ref SARIFDiagnostic::emitDiagnosticMessage to pick it up.
78+
RelatedLocationsCache.push_back({Loc, PLoc});
79+
}
80+
81+
void SARIFDiagnostic::emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc,
82+
StringRef ModuleName) {
83+
RelatedLocationsCache.push_back({Loc, PLoc});
84+
}
85+
6486
SarifResult SARIFDiagnostic::addLocationToResult(
6587
SarifResult Result, FullSourceLoc Loc, PresumedLoc PLoc,
6688
ArrayRef<CharSourceRange> Ranges, const Diagnostic &Diag) {
89+
auto Locations = getSarifLocation(Loc, PLoc, Ranges);
90+
return Result.addLocations(Locations);
91+
}
92+
93+
SarifResult SARIFDiagnostic::addRelatedLocationToResult(SarifResult Result,
94+
FullSourceLoc Loc,
95+
PresumedLoc PLoc) {
96+
auto Locations = getSarifLocation(Loc, PLoc, {});
97+
return Result.addRelatedLocations(Locations);
98+
}
99+
100+
llvm::SmallVector<CharSourceRange>
101+
SARIFDiagnostic::getSarifLocation(FullSourceLoc Loc, PresumedLoc PLoc,
102+
ArrayRef<CharSourceRange> Ranges) {
67103
SmallVector<CharSourceRange> Locations = {};
68104

69105
if (PLoc.isInvalid()) {
@@ -75,7 +111,7 @@ SarifResult SARIFDiagnostic::addLocationToResult(
75111
// FIXME(llvm-project/issues/57366): File-only locations
76112
}
77113
}
78-
return Result;
114+
return {};
79115
}
80116

81117
FileID CaretFileID = Loc.getExpansionLoc().getFileID();
@@ -127,10 +163,11 @@ SarifResult SARIFDiagnostic::addLocationToResult(
127163
SourceLocation DiagLoc = SM.translateLineCol(FID, PLoc.getLine(), ColNo);
128164

129165
// FIXME(llvm-project/issues/57366): Properly process #line directives.
130-
Locations.push_back(
131-
CharSourceRange{SourceRange{DiagLoc, DiagLoc}, /* ITR = */ false});
166+
CharSourceRange Range = {SourceRange{DiagLoc, DiagLoc}, /* ITR = */ false};
167+
if (Range.isValid())
168+
Locations.push_back(std::move(Range));
132169

133-
return Result.setLocations(Locations);
170+
return Locations;
134171
}
135172

136173
SarifRule
@@ -207,15 +244,6 @@ void SARIFDiagnostic::emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
207244
assert(false && "Not implemented in SARIF mode");
208245
}
209246

210-
void SARIFDiagnostic::emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) {
211-
assert(false && "Not implemented in SARIF mode");
212-
}
213-
214-
void SARIFDiagnostic::emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc,
215-
StringRef ModuleName) {
216-
assert(false && "Not implemented in SARIF mode");
217-
}
218-
219247
void SARIFDiagnostic::emitBuildingModuleLocation(FullSourceLoc Loc,
220248
PresumedLoc PLoc,
221249
StringRef ModuleName) {

clang/lib/Interpreter/IncrementalAction.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ bool InProcessPrintingASTConsumer::HandleTopLevelDecl(DeclGroupRef DGR) {
135135
if (DGR.isNull())
136136
return true;
137137

138+
CompilerInstance *CI = Interp.getCompilerInstance();
139+
DiagnosticsEngine &Diags = CI->getDiagnostics();
140+
if (Diags.hasErrorOccurred())
141+
return true;
142+
138143
for (Decl *D : DGR)
139144
if (auto *TLSD = llvm::dyn_cast<TopLevelStmtDecl>(D))
140145
if (TLSD && TLSD->isSemiMissing()) {

clang/lib/StaticAnalyzer/Core/SarifDiagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ SarifDiagnostics::createResult(const PathDiagnostic *Diag,
218218
.setRuleId(CheckName)
219219
.setDiagnosticMessage(Diag->getVerboseDescription())
220220
.setDiagnosticLevel(SarifResultLevel::Warning)
221-
.setLocations({Range})
221+
.addLocations({Range})
222222
.addPartialFingerprint(IssueHashKey, IssueHash)
223223
.setHostedViewerURI(HtmlReportURL)
224224
.setThreadFlows(Flows);

0 commit comments

Comments
 (0)