clang-tools 23.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
41 // Ignore functions that have been deleted.
42 Finder->addMatcher(decl(functionDecl(unless(isDeleted()))).bind("func_decl"),
43 this);
44}
45
46void InlineFunctionDeclCheck::check(const MatchFinder::MatchResult &Result) {
47 const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>("func_decl");
48
49 // Consider only explicitly or implicitly inline functions.
50 if (FuncDecl == nullptr || !FuncDecl->isInlined())
51 return;
52
53 SourceLocation SrcBegin = FuncDecl->getBeginLoc();
54
55 // If we have a template parameter list, we need to skip that because the
56 // LIBC_INLINE macro must be placed after that.
57 if (const TemplateParameterList *TemplateParams =
59 SrcBegin = TemplateParams->getRAngleLoc();
60 std::optional<Token> NextToken =
62 SrcBegin, *Result.SourceManager, Result.Context->getLangOpts());
63 if (NextToken)
64 SrcBegin = NextToken->getLocation();
65 }
66
67 // Consider functions only in header files.
68 if (!utils::isSpellingLocInHeaderFile(SrcBegin, *Result.SourceManager,
69 getHeaderFileExtensions()))
70 return;
71
72 // Ignore lambda functions as they are internal and implicit.
73 if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FuncDecl))
74 if (MethodDecl->getParent()->isLambda())
75 return;
76
77 // Check if decl starts with LIBC_INLINE
78 auto Loc = FullSourceLoc(Result.SourceManager->getFileLoc(SrcBegin),
79 *Result.SourceManager);
80 const StringRef SrcText = Loc.getBufferData().drop_front(Loc.getFileOffset());
81 if (SrcText.starts_with("LIBC_INLINE"))
82 return;
83
84 diag(SrcBegin, "%0 must be tagged with the LIBC_INLINE macro; the macro "
85 "should be placed at the beginning of the declaration")
86 << FuncDecl << FixItHint::CreateInsertion(Loc, "LIBC_INLINE ");
87}
88
89} // 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)
Definition LexerUtils.h:106
bool isSpellingLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const FileExtensionsSet &HeaderFileExtensions)
Checks whether spelling location of Loc is in header file.
static constexpr const char FuncDecl[]