clang-tools 17.0.0git
InlineFunctionDeclCheck.cpp
Go to the documentation of this file.
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
10#include "../utils/FileExtensionsUtils.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13
14#include "llvm/ADT/StringSet.h"
15
16using namespace clang::ast_matchers;
17
18namespace clang::tidy::llvm_libc {
19
21 ClangTidyContext *Context)
22 : ClangTidyCheck(Name, Context),
23 HeaderFileExtensions(Context->getHeaderFileExtensions()) {}
24
26 Finder->addMatcher(decl(functionDecl()).bind("func_decl"), this);
27}
28
29void InlineFunctionDeclCheck::check(const MatchFinder::MatchResult &Result) {
30 const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>("func_decl");
31
32 // Consider only explicitly or implicitly inline functions.
33 if (FuncDecl == nullptr || !FuncDecl->isInlined())
34 return;
35
36 SourceLocation SrcBegin = FuncDecl->getBeginLoc();
37 // Consider functions only in header files.
38 if (!utils::isSpellingLocInHeaderFile(SrcBegin, *Result.SourceManager,
39 HeaderFileExtensions))
40 return;
41
42 // Ignore lambda functions as they are internal and implicit.
43 if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FuncDecl)) {
44 if (MethodDecl->getParent()->isLambda())
45 return;
46 }
47
48 // Check if decl starts with LIBC_INLINE
49 auto Loc = FullSourceLoc(Result.SourceManager->getFileLoc(SrcBegin),
50 *Result.SourceManager);
51 llvm::StringRef SrcText = Loc.getBufferData().drop_front(Loc.getFileOffset());
52 if (SrcText.starts_with("LIBC_INLINE"))
53 return;
54
55 diag(SrcBegin, "%0 must be tagged with the LIBC_INLINE macro; the macro "
56 "should be placed at the beginning of the declaration")
57 << FuncDecl;
58}
59
60} // namespace clang::tidy::llvm_libc
SourceLocation Loc
Token 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)
bool isSpellingLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const FileExtensionsSet &HeaderFileExtensions)
Checks whether spelling location of Loc is in header file.