10#include "clang/Frontend/CompilerInstance.h"
11#include "clang/Lex/PPCallbacks.h"
12#include "clang/Lex/Preprocessor.h"
21class KernelNameRestrictionPPCallbacks :
public PPCallbacks {
23 explicit KernelNameRestrictionPPCallbacks(ClangTidyCheck &Check,
24 const SourceManager &SM)
25 : Check(Check), SM(SM) {}
27 void InclusionDirective(SourceLocation HashLoc,
const Token &IncludeTok,
28 StringRef FileName,
bool IsAngled,
29 CharSourceRange FileNameRange,
30 OptionalFileEntryRef File, StringRef SearchPath,
31 StringRef RelativePath,
const Module *SuggestedModule,
33 SrcMgr::CharacteristicKind FileType)
override;
35 void EndOfMainFile()
override;
40 bool fileNameIsRestricted(StringRef FileName);
42 struct IncludeDirective {
47 std::vector<IncludeDirective> IncludeDirectives;
48 ClangTidyCheck &Check;
49 const SourceManager &SM;
58 std::make_unique<KernelNameRestrictionPPCallbacks>(*
this, SM));
61void KernelNameRestrictionPPCallbacks::InclusionDirective(
62 SourceLocation HashLoc,
const Token &, StringRef FileName,
bool,
63 CharSourceRange, OptionalFileEntryRef, StringRef, StringRef,
const Module *,
64 bool, SrcMgr::CharacteristicKind) {
65 IncludeDirective ID = {HashLoc, FileName};
66 IncludeDirectives.push_back(std::move(ID));
69bool KernelNameRestrictionPPCallbacks::fileNameIsRestricted(
71 return FileName.equals_insensitive(
"kernel.cl") ||
72 FileName.equals_insensitive(
"verilog.cl") ||
73 FileName.equals_insensitive(
"vhdl.cl");
76void KernelNameRestrictionPPCallbacks::EndOfMainFile() {
79 OptionalFileEntryRef
Entry = SM.getFileEntryRefForID(SM.getMainFileID());
80 StringRef FileName = llvm::sys::path::filename(
Entry->getName());
81 if (fileNameIsRestricted(FileName))
82 Check.diag(SM.getLocForStartOfFile(SM.getMainFileID()),
83 "compiling '%0' may cause additional compilation errors due "
84 "to the name of the kernel source file; consider renaming the "
85 "included kernel source file")
88 if (IncludeDirectives.empty())
92 for (
const IncludeDirective &ID : IncludeDirectives) {
93 StringRef FileName = llvm::sys::path::filename(ID.FileName);
94 if (fileNameIsRestricted(FileName))
96 "including '%0' may cause additional compilation errors due "
97 "to the name of the kernel source file; consider renaming the "
98 "included kernel source file")
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *) override