clang-tools 23.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
42private:
43 enum class CommentKind {
44 None,
45 Literal,
46 NonLiteral,
47 };
48
49 const unsigned StrictMode : 1;
50 const unsigned IgnoreSingleArgument : 1;
51 const unsigned CommentAnonymousInitLists : 1;
52 const unsigned CommentBoolLiterals : 1;
53 const unsigned CommentCharacterLiterals : 1;
54 const unsigned CommentFloatLiterals : 1;
55 const unsigned CommentIntegerLiterals : 1;
56 const unsigned CommentNullPtrs : 1;
57 const unsigned CommentParenthesizedTemporaries : 1;
58 const unsigned CommentStringLiterals : 1;
59 const unsigned CommentTypedInitLists : 1;
60 const unsigned CommentUserDefinedLiterals : 1;
61 llvm::Regex IdentRE;
62
63 void checkCallArgs(ASTContext *Ctx, const FunctionDecl *Callee,
64 SourceLocation ArgBeginLoc,
65 llvm::ArrayRef<const Expr *> Args);
66
67 CommentKind shouldAddComment(const Expr *Arg) const;
68};
69
70} // namespace clang::tidy::bugprone
71
72#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
llvm::StringMap< ClangTidyValue > OptionMap