10#include "../utils/FileExtensionsUtils.h"
11#include "../utils/LexerUtils.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
15#include "llvm/ADT/StringSet.h"
23const TemplateParameterList *
24getLastTemplateParameterList(
const FunctionDecl *
FuncDecl) {
25 const TemplateParameterList *ReturnList =
26 FuncDecl->getDescribedTemplateParams();
29 const unsigned NumberOfTemplateParameterLists =
30 FuncDecl->getNumTemplateParameterLists();
32 if (NumberOfTemplateParameterLists > 0)
33 ReturnList =
FuncDecl->getTemplateParameterList(
34 NumberOfTemplateParameterLists - 1);
45 HeaderFileExtensions(Context->getHeaderFileExtensions()) {}
49 Finder->addMatcher(decl(functionDecl(unless(isDeleted()))).bind(
"func_decl"),
54 const auto *
FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>(
"func_decl");
60 SourceLocation SrcBegin =
FuncDecl->getBeginLoc();
64 if (
const TemplateParameterList *TemplateParams =
65 getLastTemplateParameterList(
FuncDecl)) {
66 SrcBegin = TemplateParams->getRAngleLoc();
67 std::optional<Token> NextToken =
69 SrcBegin, *Result.SourceManager, Result.Context->getLangOpts());
71 SrcBegin = NextToken->getLocation();
76 HeaderFileExtensions))
80 if (
const auto *MethodDecl = dyn_cast<CXXMethodDecl>(
FuncDecl))
81 if (MethodDecl->getParent()->isLambda())
85 auto Loc = FullSourceLoc(Result.SourceManager->getFileLoc(SrcBegin),
86 *Result.SourceManager);
87 llvm::StringRef SrcText =
Loc.getBufferData().drop_front(
Loc.getFileOffset());
88 if (SrcText.starts_with(
"LIBC_INLINE"))
91 diag(SrcBegin,
"%0 must be tagged with the LIBC_INLINE macro; the macro "
92 "should be placed at the beginning of the declaration")
93 <<
FuncDecl << FixItHint::CreateInsertion(
Loc,
"LIBC_INLINE ");
llvm::SmallString< 256U > Name
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
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[]