This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Initialize DiagnosticEngine in ExpandModularHeaders
ClosedPublic

Authored by PiotrZSL on Jul 23 2023, 7:51 AM.

Details

Summary

Fix issue preventing suppression of compiler warnings with
-Wno-<warning> under C++20 and above. Add call to
ProcessWarningOptions and propagate DiagnosticOpts more properly.

Fixes: #56709, #61969

Diff Detail

Event Timeline

PiotrZSL created this revision.Jul 23 2023, 7:51 AM
PiotrZSL requested review of this revision.Jul 23 2023, 7:51 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 23 2023, 7:51 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
carlosgalvezp added inline comments.Jul 23 2023, 8:42 AM
clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp
82

A bit unclear to me why we should add this line here, grepping for this function in the repo I only find hits in the clang folder. How come it's not needed in other places?

PiotrZSL added inline comments.Jul 23 2023, 8:55 AM
clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp
82

We create here new Preprocessor (line 96) and new DiagEngine (line 74), when C++20/Modules are enabled this class is register as an second Preprocessor and both are (+-) executed.
Unfortunately when we pass -Wno-macro-redefined it's pass only to original DiagEngine, and we run into situation when warning is suppressed by first DiagEngine, but not by second that is used by second Preprocessor.

Passing DiagnosticOptions alone to DiagEngine looks to be insufficient, as it's does not apply settings, only calling this function apply them. (somehow).
This is gray area for me.

More about problem here: https://discourse.llvm.org/t/rfc-expand-modular-headers-ppcallbacks-problem-in-c-20/71628

A thought came to mind - since we are doing workarounds anyway, would it be easier to ask people to simply add -clang-diagnostic* to the Checks in their config file? It's fair to assume they will get those warnings when compiling the code. I feel the more workarounds we add in the code the harder it will be to clean it up later :)

clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp
75

When downloading your patch, this seems to not be needed to make the tests pass, should it be removed?

82

Thanks for the explanation! I'm not sure what the best way forward is. Would it make sense to add some TODO or FIXME comment to further investigate in the future if we want that line of code ?

PiotrZSL added inline comments.Jul 23 2023, 1:05 PM
clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp
75

No idea, it seem reasonable.

82

Yes, FIXME could be added, but ExpandModularHeadersPPCallbacks got also other issues, for example with TargetTriple propagation and __has_builtin, looks like all those got same source, simply we shoudn't create separate Preprocessor.

carlosgalvezp added inline comments.Jul 23 2023, 11:20 PM
clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp
75

Do you mean it seems reasonable to keep it, or reasonable to remove it?

82

Agreed, the whole thing looks fishy.

PiotrZSL marked an inline comment as done.Jul 24 2023, 1:54 AM
PiotrZSL added inline comments.
clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp
75

reasonable to keep it, we want both DiagEngines to have same settings

carlosgalvezp added inline comments.Jul 24 2023, 2:29 AM
clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp
75

Reason I ask is that it seems the majority of DiagnosticOptions are initialized with default ctor:

$ git grep -E " DiagnosticOptions\(\w" | wc -l
3
$ git grep -E " DiagnosticOptions\(\)" | wc -l
74

we want both DiagEngines to have same settings

Do you know where "the other" DiagEngine is initialized? The one I can find is initialized without the compiler opts.

https://github.com/llvm/llvm-project/blob/main/clang-tools-extra/clang-tidy/ClangTidy.cpp#L544

Since the added code does not have any impact on the result of the test, I'd argue that either the test is insufficient or the added code is dead code that should be removed.

PiotrZSL marked an inline comment as done.Jul 24 2023, 2:31 AM
PiotrZSL added inline comments.
clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp
75

sure, maybe ProcessWarningOptions will override it anyway, I didnt check more deeply.

carlosgalvezp added inline comments.Jul 24 2023, 6:03 AM
clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp
75

In that case, could you please revert the change to this line, since it's not needed?

PiotrZSL marked an inline comment as done.Jul 24 2023, 6:13 AM
PiotrZSL added inline comments.
clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp
75

Yes, but later today.

carlosgalvezp accepted this revision.Jul 24 2023, 9:22 AM
carlosgalvezp added inline comments.
clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp
75

Ok! I will approve the patch now so you don't need to wait for me to land it before branching tomorrow

This revision is now accepted and ready to land.Jul 24 2023, 9:22 AM
PiotrZSL updated this revision to Diff 543601.Jul 24 2023, 9:40 AM
PiotrZSL marked 3 inline comments as done.

Review comments fixes

This revision was landed with ongoing or failed builds.Jul 24 2023, 12:49 PM
This revision was automatically updated to reflect the committed changes.