1 | //===-- InlineFunctionDeclCheck.cpp ---------------------------------------===// |
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 | |
9 | #include "InlineFunctionDeclCheck.h" |
10 | #include "../utils/FileExtensionsUtils.h" |
11 | #include "../utils/LexerUtils.h" |
12 | #include "clang/AST/ASTContext.h" |
13 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
14 | |
15 | using namespace clang::ast_matchers; |
16 | |
17 | namespace clang::tidy::llvm_libc { |
18 | |
19 | namespace { |
20 | |
21 | const TemplateParameterList * |
22 | getLastTemplateParameterList(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 | |
40 | InlineFunctionDeclCheck::InlineFunctionDeclCheck(StringRef Name, |
41 | ClangTidyContext *Context) |
42 | : ClangTidyCheck(Name, Context), |
43 | HeaderFileExtensions(Context->getHeaderFileExtensions()) {} |
44 | |
45 | void InlineFunctionDeclCheck::registerMatchers(MatchFinder *Finder) { |
46 | // Ignore functions that have been deleted. |
47 | Finder->addMatcher(NodeMatch: decl(functionDecl(unless(isDeleted()))).bind(ID: "func_decl" ), |
48 | Action: this); |
49 | } |
50 | |
51 | void InlineFunctionDeclCheck::check(const MatchFinder::MatchResult &Result) { |
52 | const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>(ID: "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 = |
66 | utils::lexer::findNextTokenSkippingComments( |
67 | Start: SrcBegin, SM: *Result.SourceManager, LangOpts: Result.Context->getLangOpts()); |
68 | if (NextToken) |
69 | SrcBegin = NextToken->getLocation(); |
70 | } |
71 | |
72 | // Consider functions only in header files. |
73 | if (!utils::isSpellingLocInHeaderFile(Loc: SrcBegin, SM&: *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>(Val: FuncDecl)) |
79 | if (MethodDecl->getParent()->isLambda()) |
80 | return; |
81 | |
82 | // Check if decl starts with LIBC_INLINE |
83 | auto Loc = FullSourceLoc(Result.SourceManager->getFileLoc(Loc: SrcBegin), |
84 | *Result.SourceManager); |
85 | llvm::StringRef SrcText = Loc.getBufferData().drop_front(Loc.getFileOffset()); |
86 | if (SrcText.starts_with(Prefix: "LIBC_INLINE" )) |
87 | return; |
88 | |
89 | diag(Loc: SrcBegin, Description: "%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(InsertionLoc: Loc, Code: "LIBC_INLINE " ); |
92 | } |
93 | |
94 | } // namespace clang::tidy::llvm_libc |
95 | |