92 lines · cpp
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-exception6//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 15using namespace clang::ast_matchers;16 17namespace clang::tidy::llvm_libc {18 19static const TemplateParameterList *20getLastTemplateParameterList(const FunctionDecl *FuncDecl) {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 36InlineFunctionDeclCheck::InlineFunctionDeclCheck(StringRef Name,37 ClangTidyContext *Context)38 : ClangTidyCheck(Name, Context),39 HeaderFileExtensions(Context->getHeaderFileExtensions()) {}40 41void InlineFunctionDeclCheck::registerMatchers(MatchFinder *Finder) {42 // Ignore functions that have been deleted.43 Finder->addMatcher(decl(functionDecl(unless(isDeleted()))).bind("func_decl"),44 this);45}46 47void InlineFunctionDeclCheck::check(const MatchFinder::MatchResult &Result) {48 const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>("func_decl");49 50 // Consider only explicitly or implicitly inline functions.51 if (FuncDecl == nullptr || !FuncDecl->isInlined())52 return;53 54 SourceLocation SrcBegin = FuncDecl->getBeginLoc();55 56 // If we have a template parameter list, we need to skip that because the57 // LIBC_INLINE macro must be placed after that.58 if (const TemplateParameterList *TemplateParams =59 getLastTemplateParameterList(FuncDecl)) {60 SrcBegin = TemplateParams->getRAngleLoc();61 std::optional<Token> NextToken =62 utils::lexer::findNextTokenSkippingComments(63 SrcBegin, *Result.SourceManager, Result.Context->getLangOpts());64 if (NextToken)65 SrcBegin = NextToken->getLocation();66 }67 68 // Consider functions only in header files.69 if (!utils::isSpellingLocInHeaderFile(SrcBegin, *Result.SourceManager,70 HeaderFileExtensions))71 return;72 73 // Ignore lambda functions as they are internal and implicit.74 if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FuncDecl))75 if (MethodDecl->getParent()->isLambda())76 return;77 78 // Check if decl starts with LIBC_INLINE79 auto Loc = FullSourceLoc(Result.SourceManager->getFileLoc(SrcBegin),80 *Result.SourceManager);81 const llvm::StringRef SrcText =82 Loc.getBufferData().drop_front(Loc.getFileOffset());83 if (SrcText.starts_with("LIBC_INLINE"))84 return;85 86 diag(SrcBegin, "%0 must be tagged with the LIBC_INLINE macro; the macro "87 "should be placed at the beginning of the declaration")88 << FuncDecl << FixItHint::CreateInsertion(Loc, "LIBC_INLINE ");89}90 91} // namespace clang::tidy::llvm_libc92