• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- AssertSideEffectCheck.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 
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_ASSERTSIDEEFFECTCHECK_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_ASSERTSIDEEFFECTCHECK_H
11 
12 #include "../ClangTidyCheck.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/StringRef.h"
15 #include <string>
16 
17 namespace clang {
18 namespace tidy {
19 namespace bugprone {
20 
21 /// Finds `assert()` with side effect.
22 ///
23 /// The condition of `assert()` is evaluated only in debug builds so a
24 /// condition with side effect can cause different behavior in debug / release
25 /// builds.
26 ///
27 /// There are two options:
28 ///
29 ///   - `AssertMacros`: A comma-separated list of the names of assert macros to
30 ///     be checked.
31 ///   - `CheckFunctionCalls`: Whether to treat non-const member and non-member
32 ///     functions as they produce side effects. Disabled by default because it
33 ///     can increase the number of false positive warnings.
34 class AssertSideEffectCheck : public ClangTidyCheck {
35 public:
36   AssertSideEffectCheck(StringRef Name, ClangTidyContext *Context);
37   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
38   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
39   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
40 
41 private:
42   const bool CheckFunctionCalls;
43   const std::string RawAssertList;
44   SmallVector<StringRef, 5> AssertMacros;
45 };
46 
47 } // namespace bugprone
48 } // namespace tidy
49 } // namespace clang
50 
51 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_ASSERTSIDEEFFECTCHECK_H
52