This is an archive of the discontinued LLVM Phabricator instance.

[clang] NFC: Extract DiagnosticOptions parsing
ClosedPublic

Authored by jansvoboda11 on Aug 30 2021, 7:39 AM.

Details

Summary

The way we parse DiagnosticOptions is a bit involved.

DiagnosticOptions are parsed as part of the cc1-parsing function CompilerInvocation::CreateFromArgs which takes DiagnosticsEngine as an argument to be able to report errors in command-line arguments. But to create DiagnosticsEngine, DiagnosticOptions are needed. This is solved by exposing the ParseDiagnosticArgs to clients and making its DiagnosticsEngine argument optional, essentially breaking the dependency cycle.

The ParseDiagnosticArgs function takes llvm::opt::ArgList &, which each client needs to create from the command-line (typically represented as std::vector<const char *>). Creating this data structure in this context is somewhat particular. This code pattern is copy-pasted in some places across the upstream code base and also in downstream repos. To make things a bit more uniform, this patch extracts the code into a new reusable function: CreateAndPopulateDiagOpts.

Diff Detail

Event Timeline

jansvoboda11 requested review of this revision.Aug 30 2021, 7:39 AM
jansvoboda11 created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptAug 30 2021, 7:39 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
jansvoboda11 edited the summary of this revision. (Show Details)Aug 30 2021, 7:39 AM
dexonsmith accepted this revision.Aug 31 2021, 8:35 AM

A few comments inline, but LGTM once you fix those.

clang/lib/Frontend/CompilerInvocation.cpp
2259–2260

Please return std::unique_ptr<DiagnosticOptions if this is an owning pointer. The caller can call .release if it needs to unpack it (although the current caller needn't do anything since there's an implicit conversion to IntrusiveRefCntPtr from std::unique_ptr).

Also, I'd prefer to propagate the lowerCamelCase style for functions recommended by the coding standards... up to you I guess since I know this file has a lot of UpperCamelCase.

2261

Please use std::make_unique<DiagnosticOptions>() instead of new.

2270–2274

I don't think -fintegrated-cc1 is related to diagnostic options. I suggest leaving it behind in the driver code and using:

std::unique_ptr<DiagnosticOptions>
clang::createAndPopulateDiagOpts(ArrayRef<const char *> Argv) {
  return createAndPopulateDiagOptsImpl(Argv);
}
This revision is now accepted and ready to land.Aug 31 2021, 8:35 AM
jansvoboda11 edited the summary of this revision. (Show Details)Sep 2 2021, 5:25 AM

Switch to unique_ptr, leave -fno-intergrated-cc1 behind.

This revision was landed with ongoing or failed builds.Sep 2 2021, 5:37 AM
This revision was automatically updated to reflect the committed changes.
jansvoboda11 added inline comments.Sep 2 2021, 5:40 AM
clang/lib/Frontend/CompilerInvocation.cpp
2270–2274

I left the -fintegrated-cc1-related code in the function, since it works on the parsed InputArgList. The whole point of this function is to free its clients of parsing the command-line options into InputArgList. If we leave -fintegrated-cc1-related code in the driver, we'll need to parse the arguments into InputArgList twice (not a big fan of that), or iterate over the raw command-line manually (not a big fan of that either, but should be more efficient). I decided to leave the code in the driver and go with the second option. Let me know it that doesn't seem reasonable to you and I can revisit this.