clang-tools 22.0.0git
InlineFunctionDeclCheck.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
11#include "../utils/LexerUtils.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14
15using namespace clang::ast_matchers;
16
17namespace clang::tidy::llvm_libc {
18
19static const TemplateParameterList *
21 const TemplateParameterList *ReturnList =
22 FuncDecl->getDescribedTemplateParams();
23
24 if (!ReturnList) {
25 const unsigned NumberOfTemplateParameterLists =
26 FuncDecl->getNumTemplateParameterLists();
27
28 if (NumberOfTemplateParameterLists > 0)
29 ReturnList = FuncDecl->getTemplateParameterList(
30 NumberOfTemplateParameterLists - 1);
31 }
32
33 return ReturnList;
34}
35
37 ClangTidyContext *Context)
38 : ClangTidyCheck(Name, Context),
39 HeaderFileExtensions(Context->getHeaderFileExtensions()) {}
40
42 // Ignore functions that have been deleted.
43 Finder->addMatcher(decl(functionDecl(unless(isDeleted()))).bind("func_decl"),
44 this);
45}
46
47void InlineFunctionDeclCheck::check(const MatchFinder::MatchResult &Result) {
48 const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>("func_decl");
49
50 // Consider only explicitly or implicitly inline functions.
51 if (FuncDecl == nullptr || !FuncDecl->isInlined())
52 return;
53
54 SourceLocation SrcBegin = FuncDecl->getBeginLoc();
55
56 // If we have a template parameter list, we need to skip that because the
57 // LIBC_INLINE macro must be placed after that.
58 if (const TemplateParameterList *TemplateParams =
60 SrcBegin = TemplateParams->getRAngleLoc();
61 std::optional<Token> NextToken =
63 SrcBegin, *Result.SourceManager, Result.Context->getLangOpts());
64 if (NextToken)
65 SrcBegin = NextToken->getLocation();
66 }
67
68 // Consider functions only in header files.
69 if (!utils::isSpellingLocInHeaderFile(SrcBegin, *Result.SourceManager,
70 HeaderFileExtensions))
71 return;
72
73 // Ignore lambda functions as they are internal and implicit.
74 if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FuncDecl))
75 if (MethodDecl->getParent()->isLambda())
76 return;
77
78 // Check if decl starts with LIBC_INLINE
79 auto Loc = FullSourceLoc(Result.SourceManager->getFileLoc(SrcBegin),
80 *Result.SourceManager);
81 llvm::StringRef SrcText = Loc.getBufferData().drop_front(Loc.getFileOffset());
82 if (SrcText.starts_with("LIBC_INLINE"))
83 return;
84
85 diag(SrcBegin, "%0 must be tagged with the LIBC_INLINE macro; the macro "
86 "should be placed at the beginning of the declaration")
87 << FuncDecl << FixItHint::CreateInsertion(Loc, "LIBC_INLINE ");
88}
89
90} // namespace clang::tidy::llvm_libc
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
InlineFunctionDeclCheck(StringRef Name, ClangTidyContext *Context)
static const TemplateParameterList * getLastTemplateParameterList(const FunctionDecl *FuncDecl)
std::optional< Token > findNextTokenSkippingComments(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
bool isSpellingLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const FileExtensionsSet &HeaderFileExtensions)
Checks whether spelling location of Loc is in header file.
static constexpr const char FuncDecl[]