Check flags always enabled or disabled code blocks in preprocessor '#if'
conditions, such as '#if 0' and '#if 1' etc.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
clang-tools-extra/clang-tidy/readability/AvoidUnconditionalPreprocessorIfCheck.cpp | ||
---|---|---|
30 | Please don't use auto unless type is spelled explicitly in same statement or iterator. | |
44 | Excessive newline. | |
clang-tools-extra/docs/ReleaseNotes.rst | ||
140 | Add etc. at the end? | |
clang-tools-extra/docs/clang-tidy/checks/readability/avoid-unconditional-preprocessor-if.rst | ||
8 | Ditto. |
There are 186 findings from this check in llvm repository, no false-positives.
Example:
llvm/lib/CodeGen/SelectionDAG/ScheduleDAGRRList.cpp:2071:2: warning: preprocessor condition is always 'true', consider removing condition but leaving its contents [readability-avoid-unconditional-preprocessor-if] clang/lib/StaticAnalyzer/Checkers/BasicObjCFoundationChecks.cpp:400:2: warning: preprocessor condition is always 'false', consider removing both the condition and its contents [readability-avoid-unconditional-preprocessor-if]
Looks good, minor comments!
clang-tools-extra/clang-tidy/readability/AvoidUnconditionalPreprocessorIfCheck.h | ||
---|---|---|
17 | Ultra nit: remove extra space | |
clang-tools-extra/test/clang-tidy/checkers/readability/avoid-unconditional-preprocessor-if.cpp | ||
18–32 | For completeness, would it make sense to add the same test but with the "is always 'false'" case? | |
68–70 | Add case for if 10 < DDD ? Also, add test case for comparing 2 macros? If people follow the "no magic numbers" policy they'll likely have defines for both sides of the comparison. |
Removed some whitespace & added tests
clang-tools-extra/test/clang-tidy/checkers/readability/avoid-unconditional-preprocessor-if.cpp | ||
---|---|---|
68–70 | I don't support currently macros, would need to check if they defined unconditionally in same file, and I didn't want to mess with this. |
clang-tools-extra/clang-tidy/readability/AvoidUnconditionalPreprocessorIfCheck.cpp | ||
---|---|---|
41 | "static" is an overloaded C++ keyword which can lead to confusion in this context (you don't intend to detect whether a token is the static keyword). Would it be possible to give this a different name? Same for isStaticToken | |
clang-tools-extra/test/clang-tidy/checkers/readability/avoid-unconditional-preprocessor-if.cpp | ||
68–70 | Yes I didn't mean "function-like macros", just a plain define like you just added. Great, thanks! |
LGTM thank you! Really useful check! I'm not very expert in the PP callbacks so I can't give much feedback, feel free to wait for other more expert reviewers if you want.
Ultra nit: remove extra space