diff options
author | Georgy Komarov <jubnzv@gmail.com> | 2021-04-06 07:57:47 +0300 |
---|---|---|
committer | Georgy Komarov <jubnzv@gmail.com> | 2021-04-22 10:14:10 +0300 |
commit | 9a930aa5bd2fc4686002d02411141a19f0ad8f36 (patch) | |
tree | 1c5a2c7f4ae165bd2d5fc94094dc0b00b48a546d | |
parent | [NewPM] Mark some more wrapper passes as ignored (diff) | |
download | llvm-project-9a930aa5bd2fc4686002d02411141a19f0ad8f36.tar.gz llvm-project-9a930aa5bd2fc4686002d02411141a19f0ad8f36.tar.bz2 llvm-project-9a930aa5bd2fc4686002d02411141a19f0ad8f36.zip |
[clang-tidy] Avoid bugprone-macro-parentheses warnings after goto argument
clang-tidy should not generate warnings for the goto argument without
parentheses, because it would be a syntax error.
The only valid case where an argument can be enclosed in parentheses is
"Labels as Values" gcc extension: https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html.
This commit adds support for the label-as-values extension as implemented in clang.
Fixes bugzilla issue 49634.
Reviewed By: aaron.ballman
Differential Revision: https://reviews.llvm.org/D99924
-rw-r--r-- | clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp | 29 | ||||
-rw-r--r-- | clang-tools-extra/test/clang-tidy/checkers/bugprone-macro-parentheses.cpp | 6 |
2 files changed, 28 insertions, 7 deletions
diff --git a/clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp index 8d4366b51a3e..303119d8ec81 100644 --- a/clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp @@ -158,6 +158,9 @@ void MacroParenthesesPPCallbacks::argument(const Token &MacroNameTok, // Skip variable declaration. bool VarDecl = possibleVarDecl(MI, MI->tokens_begin()); + // Skip the goto argument with an arbitrary number of subsequent stars. + bool FoundGoto = false; + for (auto TI = MI->tokens_begin(), TE = MI->tokens_end(); TI != TE; ++TI) { // First token. if (TI == MI->tokens_begin()) @@ -179,9 +182,17 @@ void MacroParenthesesPPCallbacks::argument(const Token &MacroNameTok, continue; } + // There should not be extra parentheses for the goto argument. + if (Tok.is(tok::kw_goto)) { + FoundGoto = true; + continue; + } + // Only interested in identifiers. - if (!Tok.isOneOf(tok::identifier, tok::raw_identifier)) + if (!Tok.isOneOf(tok::identifier, tok::raw_identifier)) { + FoundGoto = false; continue; + } // Only interested in macro arguments. if (MI->getParameterNum(Tok.getIdentifierInfo()) < 0) @@ -239,12 +250,16 @@ void MacroParenthesesPPCallbacks::argument(const Token &MacroNameTok, if (MI->isVariadic()) continue; - Check->diag(Tok.getLocation(), "macro argument should be enclosed in " - "parentheses") - << FixItHint::CreateInsertion(Tok.getLocation(), "(") - << FixItHint::CreateInsertion(Tok.getLocation().getLocWithOffset( - PP->getSpelling(Tok).length()), - ")"); + if (!FoundGoto) { + Check->diag(Tok.getLocation(), "macro argument should be enclosed in " + "parentheses") + << FixItHint::CreateInsertion(Tok.getLocation(), "(") + << FixItHint::CreateInsertion(Tok.getLocation().getLocWithOffset( + PP->getSpelling(Tok).length()), + ")"); + } + + FoundGoto = false; } } diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone-macro-parentheses.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone-macro-parentheses.cpp index 8d128352e789..6c2f42dd2dcd 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone-macro-parentheses.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone-macro-parentheses.cpp @@ -10,6 +10,10 @@ // CHECK-MESSAGES: :[[@LINE-1]]:44: warning: macro argument should be enclosed in parentheses [bugprone-macro-parentheses] #define BAD5(X) A*B=(C*)X+2 // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: macro argument should be enclosed in parentheses [bugprone-macro-parentheses] +#define BAD6(x) goto *x; +// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: macro argument should be enclosed in parentheses [bugprone-macro-parentheses] +#define BAD7(x, y) if (x) goto y; else x; +// CHECK-MESSAGES: :[[@LINE-1]]:47: warning: macro argument should be enclosed in parentheses [bugprone-macro-parentheses] #define GOOD1 1 #define GOOD2 (1+2) @@ -44,6 +48,8 @@ #define GOOD31(X) A*X=2 #define GOOD32(X) std::vector<X> #define GOOD33(x) if (!a__##x) a_##x = &f(#x) +#define GOOD34(x, y) if (x) goto y; +#define GOOD35(x, y) if (x) goto *(y); // These are allowed for now.. #define MAYBE1 *12.34 |