clang-tools 24.0.0git
ArgumentCommentCheck.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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_ARGUMENTCOMMENTCHECK_H
10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_ARGUMENTCOMMENTCHECK_H
11
12#include "../ClangTidyCheck.h"
13#include "llvm/Support/Regex.h"
14
15namespace clang::tidy::bugprone {
16
17/// Checks that argument comments match parameter names and can optionally add
18/// missing comments for literals, init-lists, and constructed temporaries.
19///
20/// The check understands argument comments in the form `/*parameter_name=*/`
21/// that are placed right before the argument.
22///
23/// \code
24/// void f(bool foo);
25///
26/// ...
27/// f(/*bar=*/true);
28/// // warning: argument name 'bar' in comment does not match parameter name
29/// 'foo'
30/// \endcode
31///
32/// The check tries to detect typos and suggest automated fixes for them. It can
33/// also insert missing comments for configured argument kinds.
35public:
36 ArgumentCommentCheck(StringRef Name, ClangTidyContext *Context);
37
38 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
39 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
40 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
41
43
44private:
45 enum class CommentKind {
46 None,
47 Literal,
48 NonLiteral,
49 };
50
51 const unsigned StrictMode : 1;
52 const unsigned IgnoreMacros : 1;
53 const unsigned IgnoreSingleArgument : 1;
54 const unsigned CommentAnonymousInitLists : 1;
55 const unsigned CommentBoolLiterals : 1;
56 const unsigned CommentCharacterLiterals : 1;
57 const unsigned CommentFloatLiterals : 1;
58 const unsigned CommentNullPtrs : 1;
59 const unsigned CommentParenthesizedTemporaries : 1;
60 const unsigned CommentStringLiterals : 1;
61 const unsigned CommentTypedInitLists : 1;
62 const unsigned CommentUserDefinedLiterals : 1;
63 const std::vector<StringRef> IgnoredParameterNames;
64 std::vector<llvm::Regex> IgnoredParameterNameRegexes;
65 const IntegerLiteralKind CommentIntegerLiterals;
66 const std::vector<StringRef> NullPtrMacros;
67 const std::vector<StringRef> BoolLiteralMacros;
68 llvm::Regex IdentRE;
69
70 void checkCallArgs(ASTContext *Ctx, const FunctionDecl *Callee,
71 SourceLocation ArgBeginLoc, SourceLocation CloseLoc,
72 llvm::ArrayRef<const Expr *> Args);
73
74 CommentKind shouldAddComment(const Expr *Arg, const ASTContext &Ctx) const;
75};
76
77} // namespace clang::tidy::bugprone
78
79namespace clang::tidy {
80template <>
81struct OptionEnumMapping<bugprone::ArgumentCommentCheck::IntegerLiteralKind> {
82 static ArrayRef<
83 std::pair<bugprone::ArgumentCommentCheck::IntegerLiteralKind, StringRef>>
85};
86} // namespace clang::tidy
87
88#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_ARGUMENTCOMMENTCHECK_H
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
ArgumentCommentCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
bugprone::ArgumentCommentCheck::IntegerLiteralKind IntegerLiteralKind
llvm::StringMap< ClangTidyValue > OptionMap
static ArrayRef< std::pair< bugprone::ArgumentCommentCheck::IntegerLiteralKind, StringRef > > getEnumMapping()
This class should be specialized by any enum type that needs to be converted to and from an llvm::Str...