This is an archive of the discontinued LLVM Phabricator instance.

WIP [clang] adds capabilities for SARIF to be written to file
Needs ReviewPublic

Authored by cjdb on Mar 3 2023, 4:13 PM.

Details

Summary

The original -fdiagnostics-format=sarif wrote directly to standard
error. This commit renames that mode to sarif-stderr and introduces a
sarif-file mode that allows the compiler to write .sarif files to
the current working directory. It also removes SARIF as an option so
that there's a single canonical approach for each option.

IMPORTANT: There are two different designs for writing SARIF to file, so I've pulled the common parts of the design out into their own patch. This ensures it only gets reviewed once, and so that the divergence is apparent in the other two patches. Once a design is settled on, this patch will be joined with the selected design.

Depends on D145201.

Diff Detail

Event Timeline

cjdb created this revision.Mar 3 2023, 4:13 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 3 2023, 4:13 PM
cjdb requested review of this revision.Mar 3 2023, 4:13 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 3 2023, 4:13 PM
cjdb updated this revision to Diff 502305.Mar 3 2023, 4:14 PM
cjdb edited the summary of this revision. (Show Details)

adds dependency

cjdb added a subscriber: denik.Mar 3 2023, 4:21 PM
cjdb updated this revision to Diff 502308.Mar 3 2023, 4:24 PM

tidies up some stuff that I overlooked

cjdb updated this revision to Diff 504309.Mar 10 2023, 5:19 PM

merges in D145438. This patch is ready for review now.

Herald added a project: Restricted Project. · View Herald TranscriptMar 10 2023, 5:19 PM

Patch topic needs "WIP" out of it I think?

Just some WIP comments, I don't believe I'd be the one to approve this anyway.

clang/include/clang/Basic/DiagnosticOptions.h
109

typically Clang uses the 'American' spelling of things.

110

I'm a touch disturbed by there being 3 very similar file names with very similar comments here. Is there a reason we need all 3?

clang/include/clang/Driver/Options.td
5850

Another 'TODO' here?

clang/include/clang/Frontend/CompilerInstance.h
610

This comment seems inaccurate.

clang/include/clang/Frontend/SARIFDiagnosticPrinter.h
39

What do we do with the DiagnosticsOptions here? Does this have to be a pointer, instead of a ref?

EDIT: I see the rest of the printers seem to do this, but I don't know if we ever have a case where these are null. I guess having them consistent here is important, but if the next person came through and made these all 'refs', it would be nice :)

clang/lib/Driver/ToolChains/Clang.cpp
4036

are we intentionally getting rid of the SARIF spellings intentionally? If so, and because we have this warning, should we report they are no longer supported?

clang/lib/Frontend/CompilerInstance.cpp
362

Oh my... I'll leave it to the Clang code owner/driver code owner (@jansvoboda11) to decide what they think of this.

clang/lib/Frontend/SARIFDiagnosticPrinter.cpp
37

Isn't TargetPath a 'Path'? If so, are you just creating a dot-file? Aren't those really nasty on Windows, and not a nice thing to do here on Linux?

45

I know these are nice/readable/etc, but llvm doesn't use these.

Also, you KNOW what EC equals here: it is a default constructed std::error_code, so the 'if' condition here is always hit. I suspect just removing the whole body and replacing with an llvm_unreachable is appropriate instead.

cjdb marked an inline comment as done.Mar 13 2023, 3:55 PM
cjdb added inline comments.
clang/include/clang/Basic/DiagnosticOptions.h
110

Agreed. I tried to get this to work with -serialize-diagnostics but that seems to activate a path that causes a different diagnostic engine to take over based on the value of DiagnosticSerializationFile. I didn't notice DiagnosticLogFile, so I'll see what happens if that is used.

clang/lib/Driver/ToolChains/Clang.cpp
4036

Yes. SARIF support isn't documented at all, and as such, I suspect the number of people who know about it (and use it for more than simple curiosity) would be hand-countable, so I'm not sure that it's worth the effort?

(The reason it's not documented is because I forgot to ask for that last year, but that's currently to our advantage.)

clang/lib/Frontend/SARIFDiagnosticPrinter.cpp
37

TargetPath should be a filename, not a directory. When implicit, this is whatever the compiler will generate as its target program. When explicit... I should probably add a check and diagnostic for this.

45

I'm not sure this code is unreachable, hence the assert message. If the file can't be opened for some reason, it's not appropriate for Clang to simply crash.

vaibhav.y added inline comments.
clang/lib/Tooling/SarifLinker.cpp
21 ↗(On Diff #504309)

Is this for link diagnostics reported as SARIF?

Hi @cjdb , I would be interested in this change also since GCC supports the same sarif-stderr and sarif-file options. Is there a further plan that it might be integrated?

@cjdb -- how would you like to proceed with this review? There's already quite a bit of discussion on the current Phab patch set, but it seems unlikely that we'd be able to wrap everything up by the Nov 15th deadline. But then again, GitHub has no ability to do stacked patches and so moving the review there might be an even bigger challenge. (No pressure, it's also totally fine to say "I have to abandon this for now".)