MacroParenthesesCheck.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. //===--- MacroParenthesesCheck.h - clang-tidy--------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_MACROPARENTHESESCHECK_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_MACROPARENTHESESCHECK_H
  10. #include "../ClangTidyCheck.h"
  11. namespace clang::tidy::bugprone {
  12. /// Finds macros that can have unexpected behaviour due to missing parentheses.
  13. ///
  14. /// Macros are expanded by the preprocessor as-is. As a result, there can be
  15. /// unexpected behaviour; operators may be evaluated in unexpected order and
  16. /// unary operators may become binary operators, etc.
  17. ///
  18. /// When the replacement list has an expression, it is recommended to surround
  19. /// it with parentheses. This ensures that the macro result is evaluated
  20. /// completely before it is used.
  21. ///
  22. /// It is also recommended to surround macro arguments in the replacement list
  23. /// with parentheses. This ensures that the argument value is calculated
  24. /// properly.
  25. class MacroParenthesesCheck : public ClangTidyCheck {
  26. public:
  27. MacroParenthesesCheck(StringRef Name, ClangTidyContext *Context)
  28. : ClangTidyCheck(Name, Context) {}
  29. void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
  30. Preprocessor *ModuleExpanderPP) override;
  31. };
  32. } // namespace clang::tidy::bugprone
  33. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_MACROPARENTHESESCHECK_H