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
19namespace {
20
21const TemplateParameterList *
22getLastTemplateParameterList(const FunctionDecl *FuncDecl) {
23 const TemplateParameterList *ReturnList =
24 FuncDecl->getDescribedTemplateParams();
25
26 if (!ReturnList) {
27 const unsigned NumberOfTemplateParameterLists =
28 FuncDecl->getNumTemplateParameterLists();
29
30 if (NumberOfTemplateParameterLists > 0)
31 ReturnList = FuncDecl->getTemplateParameterList(
32 NumberOfTemplateParameterLists - 1);
33 }
34
35 return ReturnList;
36}
37
38} // namespace
39
41 ClangTidyContext *Context)
42 : ClangTidyCheck(Name, Context),
43 HeaderFileExtensions(Context->getHeaderFileExtensions()) {}
44
46 // Ignore functions that have been deleted.
47 Finder->addMatcher(decl(functionDecl(unless(isDeleted()))).bind("func_decl"),
48 this);
49}
50
51void InlineFunctionDeclCheck::check(const MatchFinder::MatchResult &Result) {
52 const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>("func_decl");
53
54 // Consider only explicitly or implicitly inline functions.
55 if (FuncDecl == nullptr || !FuncDecl->isInlined())
56 return;
57
58 SourceLocation SrcBegin = FuncDecl->getBeginLoc();
59
60 // If we have a template parameter list, we need to skip that because the
61 // LIBC_INLINE macro must be placed after that.
62 if (const TemplateParameterList *TemplateParams =
63 getLastTemplateParameterList(FuncDecl)) {
64 SrcBegin = TemplateParams->getRAngleLoc();
65 std::optional<Token> NextToken =
67 SrcBegin, *Result.SourceManager, Result.Context->getLangOpts());
68 if (NextToken)
69 SrcBegin = NextToken->getLocation();
70 }
71
72 // Consider functions only in header files.
73 if (!utils::isSpellingLocInHeaderFile(SrcBegin, *Result.SourceManager,
74 HeaderFileExtensions))
75 return;
76
77 // Ignore lambda functions as they are internal and implicit.
78 if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FuncDecl))
79 if (MethodDecl->getParent()->isLambda())
80 return;
81
82 // Check if decl starts with LIBC_INLINE
83 auto Loc = FullSourceLoc(Result.SourceManager->getFileLoc(SrcBegin),
84 *Result.SourceManager);
85 llvm::StringRef SrcText = Loc.getBufferData().drop_front(Loc.getFileOffset());
86 if (SrcText.starts_with("LIBC_INLINE"))
87 return;
88
89 diag(SrcBegin, "%0 must be tagged with the LIBC_INLINE macro; the macro "
90 "should be placed at the beginning of the declaration")
91 << FuncDecl << FixItHint::CreateInsertion(Loc, "LIBC_INLINE ");
92}
93
94} // 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)
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[]