Author: alexfh Date: Thu Mar 15 01:25:39 2018 New Revision: 327606 URL: http://llvm.org/viewvc/llvm-project?rev=327606&view=rev Log: [clang-tidy] rename_check.py {misc,bugprone}-macro-parentheses
Added: clang-tools-extra/trunk/clang-tidy/bugprone/MacroParenthesesCheck.cpp - copied, changed from r327590, clang-tools-extra/trunk/clang-tidy/misc/MacroParenthesesCheck.cpp clang-tools-extra/trunk/clang-tidy/bugprone/MacroParenthesesCheck.h - copied, changed from r327590, clang-tools-extra/trunk/clang-tidy/misc/MacroParenthesesCheck.h clang-tools-extra/trunk/docs/clang-tidy/checks/bugprone-macro-parentheses.rst - copied, changed from r327590, clang-tools-extra/trunk/docs/clang-tidy/checks/misc-macro-parentheses.rst clang-tools-extra/trunk/test/clang-tidy/bugprone-macro-parentheses-cmdline.cpp - copied, changed from r327590, clang-tools-extra/trunk/test/clang-tidy/misc-macro-parentheses-cmdline.cpp clang-tools-extra/trunk/test/clang-tidy/bugprone-macro-parentheses.cpp - copied, changed from r327590, clang-tools-extra/trunk/test/clang-tidy/misc-macro-parentheses.cpp Removed: clang-tools-extra/trunk/clang-tidy/misc/MacroParenthesesCheck.cpp clang-tools-extra/trunk/clang-tidy/misc/MacroParenthesesCheck.h clang-tools-extra/trunk/docs/clang-tidy/checks/misc-macro-parentheses.rst clang-tools-extra/trunk/test/clang-tidy/misc-macro-parentheses-cmdline.cpp clang-tools-extra/trunk/test/clang-tidy/misc-macro-parentheses.cpp Modified: clang-tools-extra/trunk/clang-tidy/bugprone/BugproneTidyModule.cpp clang-tools-extra/trunk/clang-tidy/bugprone/CMakeLists.txt clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp clang-tools-extra/trunk/docs/ReleaseNotes.rst clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst Modified: clang-tools-extra/trunk/clang-tidy/bugprone/BugproneTidyModule.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/bugprone/BugproneTidyModule.cpp?rev=327606&r1=327605&r2=327606&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/bugprone/BugproneTidyModule.cpp (original) +++ clang-tools-extra/trunk/clang-tidy/bugprone/BugproneTidyModule.cpp Thu Mar 15 01:25:39 2018 @@ -22,6 +22,7 @@ #include "IncorrectRoundingsCheck.h" #include "IntegerDivisionCheck.h" #include "LambdaFunctionNameCheck.h" +#include "MacroParenthesesCheck.h" #include "MacroRepeatedSideEffectsCheck.h" #include "MisplacedOperatorInStrlenInAllocCheck.h" #include "MisplacedWideningCastCheck.h" @@ -73,6 +74,8 @@ public: "bugprone-integer-division"); CheckFactories.registerCheck<LambdaFunctionNameCheck>( "bugprone-lambda-function-name"); + CheckFactories.registerCheck<MacroParenthesesCheck>( + "bugprone-macro-parentheses"); CheckFactories.registerCheck<MacroRepeatedSideEffectsCheck>( "bugprone-macro-repeated-side-effects"); CheckFactories.registerCheck<MisplacedOperatorInStrlenInAllocCheck>( Modified: clang-tools-extra/trunk/clang-tidy/bugprone/CMakeLists.txt URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/bugprone/CMakeLists.txt?rev=327606&r1=327605&r2=327606&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/bugprone/CMakeLists.txt (original) +++ clang-tools-extra/trunk/clang-tidy/bugprone/CMakeLists.txt Thu Mar 15 01:25:39 2018 @@ -14,6 +14,7 @@ add_clang_library(clangTidyBugproneModul IncorrectRoundingsCheck.cpp IntegerDivisionCheck.cpp LambdaFunctionNameCheck.cpp + MacroParenthesesCheck.cpp MacroRepeatedSideEffectsCheck.cpp MisplacedOperatorInStrlenInAllocCheck.cpp MisplacedWideningCastCheck.cpp Copied: clang-tools-extra/trunk/clang-tidy/bugprone/MacroParenthesesCheck.cpp (from r327590, clang-tools-extra/trunk/clang-tidy/misc/MacroParenthesesCheck.cpp) URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/bugprone/MacroParenthesesCheck.cpp?p2=clang-tools-extra/trunk/clang-tidy/bugprone/MacroParenthesesCheck.cpp&p1=clang-tools-extra/trunk/clang-tidy/misc/MacroParenthesesCheck.cpp&r1=327590&r2=327606&rev=327606&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/misc/MacroParenthesesCheck.cpp (original) +++ clang-tools-extra/trunk/clang-tidy/bugprone/MacroParenthesesCheck.cpp Thu Mar 15 01:25:39 2018 @@ -14,7 +14,7 @@ namespace clang { namespace tidy { -namespace misc { +namespace bugprone { namespace { class MacroParenthesesPPCallbacks : public PPCallbacks { @@ -255,6 +255,6 @@ void MacroParenthesesCheck::registerPPCa &Compiler.getPreprocessor(), this)); } -} // namespace misc +} // namespace bugprone } // namespace tidy } // namespace clang Copied: clang-tools-extra/trunk/clang-tidy/bugprone/MacroParenthesesCheck.h (from r327590, clang-tools-extra/trunk/clang-tidy/misc/MacroParenthesesCheck.h) URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/bugprone/MacroParenthesesCheck.h?p2=clang-tools-extra/trunk/clang-tidy/bugprone/MacroParenthesesCheck.h&p1=clang-tools-extra/trunk/clang-tidy/misc/MacroParenthesesCheck.h&r1=327590&r2=327606&rev=327606&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/misc/MacroParenthesesCheck.h (original) +++ clang-tools-extra/trunk/clang-tidy/bugprone/MacroParenthesesCheck.h Thu Mar 15 01:25:39 2018 @@ -7,14 +7,14 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MACRO_PARENTHESES_H -#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MACRO_PARENTHESES_H +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_MACROPARENTHESESCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_MACROPARENTHESESCHECK_H #include "../ClangTidy.h" namespace clang { namespace tidy { -namespace misc { +namespace bugprone { /// Finds macros that can have unexpected behaviour due to missing parentheses. /// @@ -36,8 +36,8 @@ public: void registerPPCallbacks(CompilerInstance &Compiler) override; }; -} // namespace misc +} // namespace bugprone } // namespace tidy } // namespace clang -#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MACRO_PARENTHESES_H +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_MACROPARENTHESESCHECK_H Modified: clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt?rev=327606&r1=327605&r2=327606&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt (original) +++ clang-tools-extra/trunk/clang-tidy/misc/CMakeLists.txt Thu Mar 15 01:25:39 2018 @@ -4,7 +4,6 @@ add_clang_library(clangTidyMiscModule MisplacedConstCheck.cpp UnconventionalAssignOperatorCheck.cpp DefinitionsInHeadersCheck.cpp - MacroParenthesesCheck.cpp MiscTidyModule.cpp NewDeleteOverloadsCheck.cpp NonCopyableObjects.cpp Removed: clang-tools-extra/trunk/clang-tidy/misc/MacroParenthesesCheck.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/MacroParenthesesCheck.cpp?rev=327605&view=auto ============================================================================== --- clang-tools-extra/trunk/clang-tidy/misc/MacroParenthesesCheck.cpp (original) +++ clang-tools-extra/trunk/clang-tidy/misc/MacroParenthesesCheck.cpp (removed) @@ -1,260 +0,0 @@ -//===--- MacroParenthesesCheck.cpp - clang-tidy----------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "MacroParenthesesCheck.h" -#include "clang/Frontend/CompilerInstance.h" -#include "clang/Lex/PPCallbacks.h" -#include "clang/Lex/Preprocessor.h" - -namespace clang { -namespace tidy { -namespace misc { - -namespace { -class MacroParenthesesPPCallbacks : public PPCallbacks { -public: - MacroParenthesesPPCallbacks(Preprocessor *PP, MacroParenthesesCheck *Check) - : PP(PP), Check(Check) {} - - void MacroDefined(const Token &MacroNameTok, - const MacroDirective *MD) override { - replacementList(MacroNameTok, MD->getMacroInfo()); - argument(MacroNameTok, MD->getMacroInfo()); - } - -private: - /// Replacement list with calculations should be enclosed in parentheses. - void replacementList(const Token &MacroNameTok, const MacroInfo *MI); - - /// Arguments should be enclosed in parentheses. - void argument(const Token &MacroNameTok, const MacroInfo *MI); - - Preprocessor *PP; - MacroParenthesesCheck *Check; -}; -} // namespace - -/// Is argument surrounded properly with parentheses/braces/squares/commas? -static bool isSurroundedLeft(const Token &T) { - return T.isOneOf(tok::l_paren, tok::l_brace, tok::l_square, tok::comma, - tok::semi); -} - -/// Is argument surrounded properly with parentheses/braces/squares/commas? -static bool isSurroundedRight(const Token &T) { - return T.isOneOf(tok::r_paren, tok::r_brace, tok::r_square, tok::comma, - tok::semi); -} - -/// Is given TokenKind a keyword? -static bool isKeyword(const Token &T) { - // FIXME: better matching of keywords to avoid false positives. - return T.isOneOf(tok::kw_case, tok::kw_const, tok::kw_struct); -} - -/// Warning is written when one of these operators are not within parentheses. -static bool isWarnOp(const Token &T) { - // FIXME: This is an initial list of operators. It can be tweaked later to - // get more positives or perhaps avoid some false positive. - return T.isOneOf(tok::plus, tok::minus, tok::star, tok::slash, tok::percent, - tok::amp, tok::pipe, tok::caret); -} - -/// Is given Token a keyword that is used in variable declarations? -static bool isVarDeclKeyword(const Token &T) { - return T.isOneOf(tok::kw_bool, tok::kw_char, tok::kw_short, tok::kw_int, - tok::kw_long, tok::kw_float, tok::kw_double, tok::kw_const, - tok::kw_enum, tok::kw_inline, tok::kw_static, tok::kw_struct, - tok::kw_signed, tok::kw_unsigned); -} - -/// Is there a possible variable declaration at Tok? -static bool possibleVarDecl(const MacroInfo *MI, const Token *Tok) { - if (Tok == MI->tokens_end()) - return false; - - // If we see int/short/struct/etc., just assume this is a variable - // declaration. - if (isVarDeclKeyword(*Tok)) - return true; - - // Variable declarations start with identifier or coloncolon. - if (!Tok->isOneOf(tok::identifier, tok::raw_identifier, tok::coloncolon)) - return false; - - // Skip possible types, etc - while (Tok != MI->tokens_end() && - Tok->isOneOf(tok::identifier, tok::raw_identifier, tok::coloncolon, - tok::star, tok::amp, tok::ampamp, tok::less, - tok::greater)) - Tok++; - - // Return true for possible variable declarations. - return Tok == MI->tokens_end() || - Tok->isOneOf(tok::equal, tok::semi, tok::l_square, tok::l_paren) || - isVarDeclKeyword(*Tok); -} - -void MacroParenthesesPPCallbacks::replacementList(const Token &MacroNameTok, - const MacroInfo *MI) { - // Make sure macro replacement isn't a variable declaration. - if (possibleVarDecl(MI, MI->tokens_begin())) - return; - - // Count how deep we are in parentheses/braces/squares. - int Count = 0; - - // SourceLocation for error - SourceLocation Loc; - - for (auto TI = MI->tokens_begin(), TE = MI->tokens_end(); TI != TE; ++TI) { - const Token &Tok = *TI; - // Replacement list contains keywords, don't warn about it. - if (isKeyword(Tok)) - return; - // When replacement list contains comma/semi don't warn about it. - if (Count == 0 && Tok.isOneOf(tok::comma, tok::semi)) - return; - if (Tok.isOneOf(tok::l_paren, tok::l_brace, tok::l_square)) { - ++Count; - } else if (Tok.isOneOf(tok::r_paren, tok::r_brace, tok::r_square)) { - --Count; - // If there are unbalanced parentheses don't write any warning - if (Count < 0) - return; - } else if (Count == 0 && isWarnOp(Tok)) { - // Heuristic for macros that are clearly not intended to be enclosed in - // parentheses, macro starts with operator. For example: - // #define X *10 - if (TI == MI->tokens_begin() && (TI + 1) != TE && - !Tok.isOneOf(tok::plus, tok::minus)) - return; - // Don't warn about this macro if the last token is a star. For example: - // #define X void * - if ((TE - 1)->is(tok::star)) - return; - - Loc = Tok.getLocation(); - } - } - if (Loc.isValid()) { - const Token &Last = *(MI->tokens_end() - 1); - Check->diag(Loc, "macro replacement list should be enclosed in parentheses") - << FixItHint::CreateInsertion(MI->tokens_begin()->getLocation(), "(") - << FixItHint::CreateInsertion(Last.getLocation().getLocWithOffset( - PP->getSpelling(Last).length()), - ")"); - } -} - -void MacroParenthesesPPCallbacks::argument(const Token &MacroNameTok, - const MacroInfo *MI) { - - // Skip variable declaration. - bool VarDecl = possibleVarDecl(MI, MI->tokens_begin()); - - for (auto TI = MI->tokens_begin(), TE = MI->tokens_end(); TI != TE; ++TI) { - // First token. - if (TI == MI->tokens_begin()) - continue; - - // Last token. - if ((TI + 1) == MI->tokens_end()) - continue; - - const Token &Prev = *(TI - 1); - const Token &Next = *(TI + 1); - - const Token &Tok = *TI; - - // There should not be extra parentheses in possible variable declaration. - if (VarDecl) { - if (Tok.isOneOf(tok::equal, tok::semi, tok::l_square, tok::l_paren)) - VarDecl = false; - continue; - } - - // Only interested in identifiers. - if (!Tok.isOneOf(tok::identifier, tok::raw_identifier)) - continue; - - // Only interested in macro arguments. - if (MI->getParameterNum(Tok.getIdentifierInfo()) < 0) - continue; - - // Argument is surrounded with parentheses/squares/braces/commas. - if (isSurroundedLeft(Prev) && isSurroundedRight(Next)) - continue; - - // Don't warn after hash/hashhash or before hashhash. - if (Prev.isOneOf(tok::hash, tok::hashhash) || Next.is(tok::hashhash)) - continue; - - // Argument is a struct member. - if (Prev.isOneOf(tok::period, tok::arrow, tok::coloncolon, tok::arrowstar, - tok::periodstar)) - continue; - - // Argument is a namespace or class. - if (Next.is(tok::coloncolon)) - continue; - - // String concatenation. - if (isStringLiteral(Prev.getKind()) || isStringLiteral(Next.getKind())) - continue; - - // Type/Var. - if (isAnyIdentifier(Prev.getKind()) || isKeyword(Prev) || - isAnyIdentifier(Next.getKind()) || isKeyword(Next)) - continue; - - // Initialization. - if (Next.is(tok::l_paren)) - continue; - - // Cast. - if (Prev.is(tok::l_paren) && Next.is(tok::star) && - TI + 2 != MI->tokens_end() && (TI + 2)->is(tok::r_paren)) - continue; - - // Assignment/return, i.e. '=x;' or 'return x;'. - if (Prev.isOneOf(tok::equal, tok::kw_return) && Next.is(tok::semi)) - continue; - - // C++ template parameters. - if (PP->getLangOpts().CPlusPlus && Prev.isOneOf(tok::comma, tok::less) && - Next.isOneOf(tok::comma, tok::greater)) - continue; - - // Namespaces. - if (Prev.is(tok::kw_namespace)) - continue; - - // Variadic templates - 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()), - ")"); - } -} - -void MacroParenthesesCheck::registerPPCallbacks(CompilerInstance &Compiler) { - Compiler.getPreprocessor().addPPCallbacks( - llvm::make_unique<MacroParenthesesPPCallbacks>( - &Compiler.getPreprocessor(), this)); -} - -} // namespace misc -} // namespace tidy -} // namespace clang Removed: clang-tools-extra/trunk/clang-tidy/misc/MacroParenthesesCheck.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/MacroParenthesesCheck.h?rev=327605&view=auto ============================================================================== --- clang-tools-extra/trunk/clang-tidy/misc/MacroParenthesesCheck.h (original) +++ clang-tools-extra/trunk/clang-tidy/misc/MacroParenthesesCheck.h (removed) @@ -1,43 +0,0 @@ -//===--- MacroParenthesesCheck.h - clang-tidy--------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MACRO_PARENTHESES_H -#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MACRO_PARENTHESES_H - -#include "../ClangTidy.h" - -namespace clang { -namespace tidy { -namespace misc { - -/// Finds macros that can have unexpected behaviour due to missing parentheses. -/// -/// Macros are expanded by the preprocessor as-is. As a result, there can be -/// unexpected behaviour; operators may be evaluated in unexpected order and -/// unary operators may become binary operators, etc. -/// -/// When the replacement list has an expression, it is recommended to surround -/// it with parentheses. This ensures that the macro result is evaluated -/// completely before it is used. -/// -/// It is also recommended to surround macro arguments in the replacement list -/// with parentheses. This ensures that the argument value is calculated -/// properly. -class MacroParenthesesCheck : public ClangTidyCheck { -public: - MacroParenthesesCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} - void registerPPCallbacks(CompilerInstance &Compiler) override; -}; - -} // namespace misc -} // namespace tidy -} // namespace clang - -#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_MACRO_PARENTHESES_H Modified: clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp?rev=327606&r1=327605&r2=327606&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp (original) +++ clang-tools-extra/trunk/clang-tidy/misc/MiscTidyModule.cpp Thu Mar 15 01:25:39 2018 @@ -11,7 +11,6 @@ #include "../ClangTidyModule.h" #include "../ClangTidyModuleRegistry.h" #include "DefinitionsInHeadersCheck.h" -#include "MacroParenthesesCheck.h" #include "MisplacedConstCheck.h" #include "NewDeleteOverloadsCheck.h" #include "NonCopyableObjects.h" @@ -39,8 +38,6 @@ public: "misc-unconventional-assign-operator"); CheckFactories.registerCheck<DefinitionsInHeadersCheck>( "misc-definitions-in-headers"); - CheckFactories.registerCheck<MacroParenthesesCheck>( - "misc-macro-parentheses"); CheckFactories.registerCheck<NewDeleteOverloadsCheck>( "misc-new-delete-overloads"); CheckFactories.registerCheck<NonCopyableObjectsCheck>( Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=327606&r1=327605&r2=327606&view=diff ============================================================================== --- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original) +++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Thu Mar 15 01:25:39 2018 @@ -94,12 +94,12 @@ Improvements to clang-tidy Warns if global, non-trivial objects with static storage are constructed, unless the object is statically initialized with a ``constexpr`` constructor or has no explicit constructor. - + - New `fuchsia-trailing-return <http://clang.llvm.org/extra/clang-tidy/checks/fuchsia-trailing-return.html>`_ check - Functions that have trailing returns are disallowed, except for those - using ``decltype`` specifiers and lambda with otherwise unutterable + Functions that have trailing returns are disallowed, except for those + using ``decltype`` specifiers and lambda with otherwise unutterable return types. - New `modernize-use-uncaught-exceptions @@ -128,6 +128,9 @@ Improvements to clang-tidy - The 'misc-lambda-function-name' check was renamed to `bugprone-lambda-function-name <http://clang.llvm.org/extra/clang-tidy/checks/bugprone-lambda-function-name.html>`_ +- The 'misc-macro-parentheses' check was renamed to `bugprone-macro-parentheses + <http://clang.llvm.org/extra/clang-tidy/checks/bugprone-macro-parentheses.html>`_ + - The 'misc-macro-repeated-side-effects' check was renamed to `bugprone-macro-repeated-side-effects <http://clang.llvm.org/extra/clang-tidy/checks/bugprone-macro-repeated-side-effects.html>`_ Copied: clang-tools-extra/trunk/docs/clang-tidy/checks/bugprone-macro-parentheses.rst (from r327590, clang-tools-extra/trunk/docs/clang-tidy/checks/misc-macro-parentheses.rst) URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/bugprone-macro-parentheses.rst?p2=clang-tools-extra/trunk/docs/clang-tidy/checks/bugprone-macro-parentheses.rst&p1=clang-tools-extra/trunk/docs/clang-tidy/checks/misc-macro-parentheses.rst&r1=327590&r2=327606&rev=327606&view=diff ============================================================================== --- clang-tools-extra/trunk/docs/clang-tidy/checks/misc-macro-parentheses.rst (original) +++ clang-tools-extra/trunk/docs/clang-tidy/checks/bugprone-macro-parentheses.rst Thu Mar 15 01:25:39 2018 @@ -1,7 +1,7 @@ -.. title:: clang-tidy - misc-macro-parentheses +.. title:: clang-tidy - bugprone-macro-parentheses -misc-macro-parentheses -====================== +bugprone-macro-parentheses +========================== Finds macros that can have unexpected behaviour due to missing parentheses. Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst?rev=327606&r1=327605&r2=327606&view=diff ============================================================================== --- clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst (original) +++ clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst Thu Mar 15 01:25:39 2018 @@ -30,6 +30,7 @@ Clang-Tidy Checks bugprone-incorrect-roundings bugprone-integer-division bugprone-lambda-function-name + bugprone-macro-parentheses bugprone-macro-repeated-side-effects bugprone-misplaced-operator-in-strlen-in-alloc bugprone-misplaced-widening-cast @@ -140,7 +141,6 @@ Clang-Tidy Checks llvm-namespace-comment llvm-twine-local misc-definitions-in-headers - misc-macro-parentheses misc-misplaced-const misc-new-delete-overloads misc-non-copyable-objects Removed: clang-tools-extra/trunk/docs/clang-tidy/checks/misc-macro-parentheses.rst URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/misc-macro-parentheses.rst?rev=327605&view=auto ============================================================================== --- clang-tools-extra/trunk/docs/clang-tidy/checks/misc-macro-parentheses.rst (original) +++ clang-tools-extra/trunk/docs/clang-tidy/checks/misc-macro-parentheses.rst (removed) @@ -1,19 +0,0 @@ -.. title:: clang-tidy - misc-macro-parentheses - -misc-macro-parentheses -====================== - - -Finds macros that can have unexpected behaviour due to missing parentheses. - -Macros are expanded by the preprocessor as-is. As a result, there can be -unexpected behaviour; operators may be evaluated in unexpected order and -unary operators may become binary operators, etc. - -When the replacement list has an expression, it is recommended to surround -it with parentheses. This ensures that the macro result is evaluated -completely before it is used. - -It is also recommended to surround macro arguments in the replacement list -with parentheses. This ensures that the argument value is calculated -properly. Copied: clang-tools-extra/trunk/test/clang-tidy/bugprone-macro-parentheses-cmdline.cpp (from r327590, clang-tools-extra/trunk/test/clang-tidy/misc-macro-parentheses-cmdline.cpp) URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/bugprone-macro-parentheses-cmdline.cpp?p2=clang-tools-extra/trunk/test/clang-tidy/bugprone-macro-parentheses-cmdline.cpp&p1=clang-tools-extra/trunk/test/clang-tidy/misc-macro-parentheses-cmdline.cpp&r1=327590&r2=327606&rev=327606&view=diff ============================================================================== --- clang-tools-extra/trunk/test/clang-tidy/misc-macro-parentheses-cmdline.cpp (original) +++ clang-tools-extra/trunk/test/clang-tidy/bugprone-macro-parentheses-cmdline.cpp Thu Mar 15 01:25:39 2018 @@ -1,4 +1,4 @@ -// RUN: %check_clang_tidy %s misc-macro-parentheses %t -- -- -DVAL=0+0 +// RUN: %check_clang_tidy %s bugprone-macro-parentheses %t -- -- -DVAL=0+0 // The previous command-line is producing warnings and fixes with the source // locations from a virtual buffer. VAL is replaced by '0+0'. Copied: clang-tools-extra/trunk/test/clang-tidy/bugprone-macro-parentheses.cpp (from r327590, clang-tools-extra/trunk/test/clang-tidy/misc-macro-parentheses.cpp) URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/bugprone-macro-parentheses.cpp?p2=clang-tools-extra/trunk/test/clang-tidy/bugprone-macro-parentheses.cpp&p1=clang-tools-extra/trunk/test/clang-tidy/misc-macro-parentheses.cpp&r1=327590&r2=327606&rev=327606&view=diff ============================================================================== --- clang-tools-extra/trunk/test/clang-tidy/misc-macro-parentheses.cpp (original) +++ clang-tools-extra/trunk/test/clang-tidy/bugprone-macro-parentheses.cpp Thu Mar 15 01:25:39 2018 @@ -1,15 +1,15 @@ -// RUN: %check_clang_tidy %s misc-macro-parentheses %t +// RUN: %check_clang_tidy %s bugprone-macro-parentheses %t #define BAD1 -1 -// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: macro replacement list should be enclosed in parentheses [misc-macro-parentheses] +// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: macro replacement list should be enclosed in parentheses [bugprone-macro-parentheses] #define BAD2 1+2 -// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: macro replacement list should be enclosed in parentheses [misc-macro-parentheses] +// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: macro replacement list should be enclosed in parentheses [bugprone-macro-parentheses] #define BAD3(A) (A+1) -// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: macro argument should be enclosed in parentheses [misc-macro-parentheses] +// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: macro argument should be enclosed in parentheses [bugprone-macro-parentheses] #define BAD4(x) ((unsigned char)(x & 0xff)) -// CHECK-MESSAGES: :[[@LINE-1]]:44: warning: macro argument should be enclosed in parentheses [misc-macro-parentheses] +// 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 [misc-macro-parentheses] +// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: macro argument should be enclosed in parentheses [bugprone-macro-parentheses] #define GOOD1 1 #define GOOD2 (1+2) Removed: clang-tools-extra/trunk/test/clang-tidy/misc-macro-parentheses-cmdline.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-macro-parentheses-cmdline.cpp?rev=327605&view=auto ============================================================================== --- clang-tools-extra/trunk/test/clang-tidy/misc-macro-parentheses-cmdline.cpp (original) +++ clang-tools-extra/trunk/test/clang-tidy/misc-macro-parentheses-cmdline.cpp (removed) @@ -1,10 +0,0 @@ -// RUN: %check_clang_tidy %s misc-macro-parentheses %t -- -- -DVAL=0+0 - -// The previous command-line is producing warnings and fixes with the source -// locations from a virtual buffer. VAL is replaced by '0+0'. -// Fixes could not be applied and should not be reported. -int foo() { return VAL; } - -#define V 0+0 -int bar() { return V; } -// CHECK-FIXES: #define V (0+0) Removed: clang-tools-extra/trunk/test/clang-tidy/misc-macro-parentheses.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-macro-parentheses.cpp?rev=327605&view=auto ============================================================================== --- clang-tools-extra/trunk/test/clang-tidy/misc-macro-parentheses.cpp (original) +++ clang-tools-extra/trunk/test/clang-tidy/misc-macro-parentheses.cpp (removed) @@ -1,49 +0,0 @@ -// RUN: %check_clang_tidy %s misc-macro-parentheses %t - -#define BAD1 -1 -// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: macro replacement list should be enclosed in parentheses [misc-macro-parentheses] -#define BAD2 1+2 -// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: macro replacement list should be enclosed in parentheses [misc-macro-parentheses] -#define BAD3(A) (A+1) -// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: macro argument should be enclosed in parentheses [misc-macro-parentheses] -#define BAD4(x) ((unsigned char)(x & 0xff)) -// CHECK-MESSAGES: :[[@LINE-1]]:44: warning: macro argument should be enclosed in parentheses [misc-macro-parentheses] -#define BAD5(X) A*B=(C*)X+2 -// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: macro argument should be enclosed in parentheses [misc-macro-parentheses] - -#define GOOD1 1 -#define GOOD2 (1+2) -#define GOOD3(A) #A -#define GOOD4(A,B) A ## B -#define GOOD5(T) ((T*)0) -#define GOOD6(B) "A" B "C" -#define GOOD7(b) A b -#define GOOD8(a) a B -#define GOOD9(type) (type(123)) -#define GOOD10(car, ...) car -#define GOOD11 a[b+c] -#define GOOD12(x) a[x] -#define GOOD13(x) a.x -#define GOOD14(x) a->x -#define GOOD15(x) ({ int a = x; a+4; }) -#define GOOD16(x) a_ ## x, b_ ## x = c_ ## x - 1, -#define GOOD17 case 123: x=4+5; break; -#define GOOD18(x) ;x; -#define GOOD19 ;-2; -#define GOOD20 void* -#define GOOD21(a) case Fred::a: -#define GOOD22(a) if (verbose) return a; -#define GOOD23(type) (type::Field) -#define GOOD24(t) std::set<t> s -#define GOOD25(t) std::set<t,t,t> s -#define GOOD26(x) (a->*x) -#define GOOD27(x) (a.*x) -#define GOOD28(x) namespace x {int b;} -#define GOOD29(...) std::cout << __VA_ARGS__; -#define GOOD30(args...) std::cout << args; -#define GOOD31(X) A*X=2 -#define GOOD32(X) std::vector<X> - -// These are allowed for now.. -#define MAYBE1 *12.34 -#define MAYBE2 <<3 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits