clang-tools 22.0.0git
RedundantInlineSpecifierCheck.cpp
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
10#include "clang/AST/ASTContext.h"
11#include "clang/AST/Decl.h"
12#include "clang/AST/DeclCXX.h"
13#include "clang/AST/DeclTemplate.h"
14#include "clang/AST/ExprCXX.h"
15#include "clang/ASTMatchers/ASTMatchers.h"
16#include "clang/Basic/Diagnostic.h"
17#include "clang/Basic/SourceLocation.h"
18#include "clang/Basic/SourceManager.h"
19#include "clang/Lex/Lexer.h"
20#include "clang/Lex/Token.h"
21
22using namespace clang::ast_matchers;
23
25
26namespace {
27AST_POLYMORPHIC_MATCHER(isInlineSpecified,
28 AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
29 VarDecl)) {
30 if (const auto *FD = dyn_cast<FunctionDecl>(&Node))
31 return FD->isInlineSpecified();
32 if (const auto *VD = dyn_cast<VarDecl>(&Node))
33 return VD->isInlineSpecified();
34 llvm_unreachable("Not a valid polymorphic type");
35}
36
37AST_POLYMORPHIC_MATCHER_P(isInternalLinkage,
38 AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
39 VarDecl),
40 bool, StrictMode) {
41 if (!StrictMode)
42 return false;
43 if (const auto *FD = dyn_cast<FunctionDecl>(&Node))
44 return FD->getStorageClass() == SC_Static || FD->isInAnonymousNamespace();
45 if (const auto *VD = dyn_cast<VarDecl>(&Node))
46 return VD->isInAnonymousNamespace();
47 llvm_unreachable("Not a valid polymorphic type");
48}
49} // namespace
50
51static SourceLocation getInlineTokenLocation(SourceRange RangeLocation,
52 const SourceManager &Sources,
53 const LangOptions &LangOpts) {
54 const SourceLocation Loc = RangeLocation.getBegin();
55 if (Loc.isMacroID())
56 return {};
57
58 Token FirstToken;
59 Lexer::getRawToken(Loc, FirstToken, Sources, LangOpts, true);
60 std::optional<Token> CurrentToken = FirstToken;
61 while (CurrentToken && CurrentToken->getLocation() < RangeLocation.getEnd() &&
62 CurrentToken->isNot(tok::eof)) {
63 if (CurrentToken->is(tok::raw_identifier) &&
64 CurrentToken->getRawIdentifier() == "inline")
65 return CurrentToken->getLocation();
66
67 CurrentToken =
68 Lexer::findNextToken(CurrentToken->getLocation(), Sources, LangOpts);
69 }
70 return {};
71}
72
74 const auto IsPartOfRecordDecl = hasAncestor(recordDecl());
75 Finder->addMatcher(
76 functionDecl(isInlineSpecified(),
77 anyOf(isConstexpr(), isDeleted(),
78 allOf(isDefaulted(), IsPartOfRecordDecl),
79 isInternalLinkage(StrictMode),
80 allOf(isDefinition(), IsPartOfRecordDecl)))
81 .bind("fun_decl"),
82 this);
83
84 if (StrictMode)
85 Finder->addMatcher(
86 functionTemplateDecl(
87 has(functionDecl(allOf(isInlineSpecified(), isDefinition()))))
88 .bind("templ_decl"),
89 this);
90
91 if (getLangOpts().CPlusPlus17) {
92 Finder->addMatcher(
93 varDecl(
94 isInlineSpecified(),
95 anyOf(allOf(isInternalLinkage(StrictMode),
96 unless(allOf(hasInitializer(expr()), IsPartOfRecordDecl,
97 isStaticStorageClass()))),
98 allOf(isConstexpr(), IsPartOfRecordDecl)))
99 .bind("var_decl"),
100 this);
101 }
102}
103
104template <typename T>
105void RedundantInlineSpecifierCheck::handleMatchedDecl(
106 const T *MatchedDecl, const SourceManager &Sources,
107 const MatchFinder::MatchResult &Result, StringRef Message) {
108 const SourceLocation Loc = getInlineTokenLocation(
109 MatchedDecl->getSourceRange(), Sources, Result.Context->getLangOpts());
110 if (Loc.isValid())
111 diag(Loc, Message) << MatchedDecl << FixItHint::CreateRemoval(Loc);
112}
113
115 const MatchFinder::MatchResult &Result) {
116 const SourceManager &Sources = *Result.SourceManager;
117
118 if (const auto *MatchedDecl =
119 Result.Nodes.getNodeAs<FunctionDecl>("fun_decl")) {
120 handleMatchedDecl(
121 MatchedDecl, Sources, Result,
122 "function %0 has inline specifier but is implicitly inlined");
123 } else if (const auto *MatchedDecl =
124 Result.Nodes.getNodeAs<VarDecl>("var_decl")) {
125 handleMatchedDecl(
126 MatchedDecl, Sources, Result,
127 "variable %0 has inline specifier but is implicitly inlined");
128 } else if (const auto *MatchedDecl =
129 Result.Nodes.getNodeAs<FunctionTemplateDecl>("templ_decl")) {
130 handleMatchedDecl(
131 MatchedDecl, Sources, Result,
132 "function %0 has inline specifier but is implicitly inlined");
133 }
134}
135
136} // namespace clang::tidy::readability
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
AST_POLYMORPHIC_MATCHER(isInAbseilFile, AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc, NestedNameSpecifierLoc))
Matches AST nodes that were found within Abseil files.
static SourceLocation getInlineTokenLocation(SourceRange RangeLocation, const SourceManager &Sources, const LangOptions &LangOpts)