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