21AST_MATCHER(Decl, isFromStdNamespaceOrSystemHeader) {
22 if (
const auto *D = Node.getDeclContext()->getEnclosingNamespaceContext())
23 if (D->isStdNamespace())
25 if (Node.getLocation().isInvalid())
27 return Node.getASTContext().getSourceManager().isInSystemHeader(
35 StrictMode(Options.get(
"StrictMode", false)),
36 IgnoreSingleArgument(Options.get(
"IgnoreSingleArgument", false)),
37 CommentBoolLiterals(Options.get(
"CommentBoolLiterals", false)),
38 CommentIntegerLiterals(Options.get(
"CommentIntegerLiterals", false)),
39 CommentFloatLiterals(Options.get(
"CommentFloatLiterals", false)),
40 CommentStringLiterals(Options.get(
"CommentStringLiterals", false)),
41 CommentUserDefinedLiterals(
42 Options.get(
"CommentUserDefinedLiterals", false)),
43 CommentCharacterLiterals(Options.get(
"CommentCharacterLiterals", false)),
44 CommentNullPtrs(Options.get(
"CommentNullPtrs", false)),
45 IdentRE(
"^(/\\* *)([_A-Za-z][_A-Za-z0-9]*)( *= *\\*/)$") {}
48 Options.store(Opts,
"StrictMode", StrictMode);
49 Options.store(Opts,
"IgnoreSingleArgument", IgnoreSingleArgument);
50 Options.store(Opts,
"CommentBoolLiterals", CommentBoolLiterals);
51 Options.store(Opts,
"CommentIntegerLiterals", CommentIntegerLiterals);
52 Options.store(Opts,
"CommentFloatLiterals", CommentFloatLiterals);
53 Options.store(Opts,
"CommentStringLiterals", CommentStringLiterals);
54 Options.store(Opts,
"CommentUserDefinedLiterals", CommentUserDefinedLiterals);
55 Options.store(Opts,
"CommentCharacterLiterals", CommentCharacterLiterals);
56 Options.store(Opts,
"CommentNullPtrs", CommentNullPtrs);
61 callExpr(unless(cxxOperatorCallExpr()), unless(userDefinedLiteral()),
65 unless(hasDeclaration(functionDecl(
66 hasAnyName(
"NewCallback",
"NewPermanentCallback")))),
71 unless(hasDeclaration(isFromStdNamespaceOrSystemHeader())))
74 Finder->addMatcher(cxxConstructExpr(unless(hasDeclaration(
75 isFromStdNamespaceOrSystemHeader())))
80static std::vector<std::pair<SourceLocation, StringRef>>
82 std::vector<std::pair<SourceLocation, StringRef>> Comments;
83 auto &SM = Ctx->getSourceManager();
84 const std::pair<FileID, unsigned> BeginLoc =
85 SM.getDecomposedLoc(Range.getBegin()),
87 SM.getDecomposedLoc(Range.getEnd());
89 if (BeginLoc.first != EndLoc.first)
93 const StringRef Buffer = SM.getBufferData(BeginLoc.first, &Invalid);
97 const char *StrData = Buffer.data() + BeginLoc.second;
99 Lexer TheLexer(SM.getLocForStartOfFile(BeginLoc.first), Ctx->getLangOpts(),
100 Buffer.begin(), StrData, Buffer.end());
101 TheLexer.SetCommentRetentionState(
true);
105 if (TheLexer.LexFromRawLexer(Tok))
107 if (Tok.getLocation() == Range.getEnd() || Tok.is(tok::eof))
110 if (Tok.is(tok::comment)) {
111 const std::pair<FileID, unsigned> CommentLoc =
112 SM.getDecomposedLoc(Tok.getLocation());
113 assert(CommentLoc.first == BeginLoc.first);
114 Comments.emplace_back(
116 StringRef(Buffer.begin() + CommentLoc.second, Tok.getLength()));
126static std::vector<std::pair<SourceLocation, StringRef>>
128 std::vector<std::pair<SourceLocation, StringRef>> Comments;
129 while (Loc.isValid()) {
131 Loc, Ctx->getSourceManager(), Ctx->getLangOpts(),
133 if (Tok.isNot(tok::comment))
135 Loc = Tok.getLocation();
136 Comments.emplace_back(
138 Lexer::getSourceText(CharSourceRange::getCharRange(
139 Loc, Loc.getLocWithOffset(Tok.getLength())),
140 Ctx->getSourceManager(), Ctx->getLangOpts()));
146 StringRef
ArgName,
unsigned ArgIndex) {
147 const std::string ArgNameLowerStr =
ArgName.lower();
148 const StringRef ArgNameLower = ArgNameLowerStr;
150 const unsigned UpperBound = ((
ArgName.size() + 2) / 3) + 1;
151 const unsigned ThisED = ArgNameLower.edit_distance(
152 Params[ArgIndex]->getIdentifier()->
getName().lower(),
154 if (ThisED >= UpperBound)
157 for (
unsigned I = 0, E = Params.size(); I != E; ++I) {
160 const IdentifierInfo *II = Params[I]->getIdentifier();
164 const unsigned Threshold = 2;
168 const unsigned OtherED = ArgNameLower.edit_distance(
169 II->getName().lower(),
170 true, ThisED + Threshold);
171 if (OtherED < ThisED + Threshold)
178static bool sameName(StringRef InComment, StringRef InDecl,
bool StrictMode) {
180 return InComment == InDecl;
181 InComment = InComment.trim(
'_');
182 InDecl = InDecl.trim(
'_');
184 return InComment.compare_insensitive(InDecl) == 0;
188 return Expect !=
nullptr && Expect->getLocation().isMacroID() &&
189 Expect->getNameInfo().getName().isIdentifier() &&
190 Expect->getName().starts_with(
"gmock_");
193 const CXXMethodDecl *Expect) {
195 return Mock !=
nullptr && Mock->getNextDeclInContext() == Expect &&
196 Mock->getNumParams() == Expect->getNumParams() &&
197 Mock->getLocation().isMacroID() &&
198 Mock->getNameInfo().getName().isIdentifier() &&
199 Mock->getName() == Expect->getName().substr(strlen(
"gmock_"));
209 const DeclContext *Ctx = Method->getDeclContext();
210 if (Ctx ==
nullptr || !Ctx->isRecord())
212 for (
const auto *D : Ctx->decls()) {
213 if (D->getNextDeclInContext() == Method) {
214 const auto *Previous = dyn_cast<CXXMethodDecl>(D);
220 if (
const auto *Next =
221 dyn_cast_or_null<CXXMethodDecl>(Method->getNextDeclInContext())) {
233 if (
const auto *Method = dyn_cast<CXXMethodDecl>(Func)) {
237 if (MockedMethod->size_overridden_methods() > 0)
238 return *MockedMethod->begin_overridden_methods();
247bool ArgumentCommentCheck::shouldAddComment(
const Expr *Arg)
const {
248 Arg = Arg->IgnoreImpCasts();
249 if (isa<UnaryOperator>(Arg))
250 Arg = cast<UnaryOperator>(Arg)->getSubExpr();
251 if (Arg->getExprLoc().isMacroID())
253 return (CommentBoolLiterals && isa<CXXBoolLiteralExpr>(Arg)) ||
254 (CommentIntegerLiterals && isa<IntegerLiteral>(Arg)) ||
255 (CommentFloatLiterals && isa<FloatingLiteral>(Arg)) ||
256 (CommentUserDefinedLiterals && isa<UserDefinedLiteral>(Arg)) ||
257 (CommentCharacterLiterals && isa<CharacterLiteral>(Arg)) ||
258 (CommentStringLiterals && isa<StringLiteral>(Arg)) ||
259 (CommentNullPtrs && isa<CXXNullPtrLiteralExpr>(Arg));
262void ArgumentCommentCheck::checkCallArgs(ASTContext *Ctx,
263 const FunctionDecl *OriginalCallee,
264 SourceLocation ArgBeginLoc,
265 llvm::ArrayRef<const Expr *> Args) {
266 const FunctionDecl *Callee =
resolveMocks(OriginalCallee);
270 Callee = Callee->getFirstDecl();
271 const unsigned NumArgs =
272 std::min<unsigned>(Args.size(), Callee->getNumParams());
273 if ((NumArgs == 0) || (IgnoreSingleArgument && NumArgs == 1))
276 auto MakeFileCharRange = [Ctx](SourceLocation Begin, SourceLocation End) {
277 return Lexer::makeFileCharRange(CharSourceRange::getCharRange(Begin, End),
278 Ctx->getSourceManager(),
282 for (
unsigned I = 0; I < NumArgs; ++I) {
283 const ParmVarDecl *PVD = Callee->getParamDecl(I);
284 const IdentifierInfo *II = PVD->getIdentifier();
287 if (FunctionDecl *Template = Callee->getTemplateInstantiationPattern()) {
292 if (Template->getNumParams() <= I ||
293 Template->getParamDecl(I)->isParameterPack()) {
298 const CharSourceRange BeforeArgument =
299 MakeFileCharRange(ArgBeginLoc, Args[I]->getBeginLoc());
300 ArgBeginLoc = Args[I]->getEndLoc();
302 std::vector<std::pair<SourceLocation, StringRef>> Comments;
303 if (BeforeArgument.isValid()) {
307 const CharSourceRange ArgsRange =
308 MakeFileCharRange(Args[I]->getBeginLoc(), Args[I]->getEndLoc());
312 for (
auto Comment : Comments) {
313 llvm::SmallVector<StringRef, 2> Matches;
314 if (IdentRE.match(Comment.second, &Matches) &&
315 !
sameName(Matches[2], II->getName(), StrictMode)) {
317 const DiagnosticBuilder Diag =
318 diag(Comment.first,
"argument name '%0' in comment does not "
319 "match parameter name %1")
321 if (
isLikelyTypo(Callee->parameters(), Matches[2], I)) {
322 Diag << FixItHint::CreateReplacement(
323 Comment.first, (Matches[1] + II->getName() + Matches[3]).str());
326 diag(PVD->getLocation(),
"%0 declared here", DiagnosticIDs::Note) << II;
327 if (OriginalCallee != Callee) {
328 diag(OriginalCallee->getLocation(),
329 "actual callee (%0) is declared here", DiagnosticIDs::Note)
336 if (Comments.empty() && shouldAddComment(Args[I])) {
337 const std::string ArgComment =
338 (llvm::Twine(
"/*") + II->getName() +
"=*/").str();
339 const DiagnosticBuilder Diag =
340 diag(Args[I]->getBeginLoc(),
341 "argument comment missing for literal argument %0")
343 << FixItHint::CreateInsertion(Args[I]->getBeginLoc(), ArgComment);
349 const auto *E = Result.Nodes.getNodeAs<Expr>(
"expr");
350 if (
const auto *Call = dyn_cast<CallExpr>(E)) {
351 const FunctionDecl *Callee = Call->getDirectCallee();
355 checkCallArgs(Result.Context, Callee, Call->getCallee()->getEndLoc(),
356 llvm::ArrayRef(Call->getArgs(), Call->getNumArgs()));
358 const auto *Construct = cast<CXXConstructExpr>(E);
359 if (Construct->getNumArgs() > 0 &&
360 Construct->getArg(0)->getSourceRange() == Construct->getSourceRange()) {
365 Result.Context, Construct->getConstructor(),
366 Construct->getParenOrBraceRange().getBegin(),
367 llvm::ArrayRef(Construct->getArgs(), Construct->getNumArgs()));
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.