10#include "clang/Frontend/CompilerInstance.h"
11#include "clang/Lex/PPCallbacks.h"
12#include "clang/Lex/Preprocessor.h"
22class KernelNameRestrictionPPCallbacks :
public PPCallbacks {
24 explicit KernelNameRestrictionPPCallbacks(ClangTidyCheck &Check,
25 const SourceManager &SM)
26 : Check(Check), SM(SM) {}
28 void InclusionDirective(SourceLocation HashLoc,
const Token &IncludeTok,
30 CharSourceRange FileNameRange,
31 OptionalFileEntryRef File, StringRef SearchPath,
32 StringRef RelativePath,
const Module *SuggestedModule,
34 SrcMgr::CharacteristicKind FileType)
override;
36 void EndOfMainFile()
override;
41 bool fileNameIsRestricted(StringRef
FileName);
43 struct IncludeDirective {
48 std::vector<IncludeDirective> IncludeDirectives;
49 ClangTidyCheck &Check;
50 const SourceManager &SM;
59 std::make_unique<KernelNameRestrictionPPCallbacks>(*
this, SM));
62void KernelNameRestrictionPPCallbacks::InclusionDirective(
63 SourceLocation HashLoc,
const Token &, StringRef
FileName,
bool,
64 CharSourceRange, OptionalFileEntryRef, StringRef, StringRef,
const Module *,
65 bool, SrcMgr::CharacteristicKind) {
67 IncludeDirectives.push_back(std::move(
ID));
70bool KernelNameRestrictionPPCallbacks::fileNameIsRestricted(
72 return FileName.equals_insensitive(
"kernel.cl") ||
73 FileName.equals_insensitive(
"verilog.cl") ||
74 FileName.equals_insensitive(
"vhdl.cl");
77void KernelNameRestrictionPPCallbacks::EndOfMainFile() {
80 OptionalFileEntryRef
Entry = SM.getFileEntryRefForID(SM.getMainFileID());
81 StringRef
FileName = llvm::sys::path::filename(
Entry->getName());
83 Check.diag(SM.getLocForStartOfFile(SM.getMainFileID()),
84 "compiling '%0' may cause additional compilation errors due "
85 "to the name of the kernel source file; consider renaming the "
86 "included kernel source file")
89 if (IncludeDirectives.empty())
93 for (
const IncludeDirective &
ID : IncludeDirectives) {
94 StringRef
FileName = llvm::sys::path::filename(
ID.FileName);
97 "including '%0' may cause additional compilation errors due "
98 "to the name of the kernel source file; consider renaming the "
99 "included kernel source file")
bool IsAngled
true if this was an include with angle brackets
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *) override
Override this to register PPCallbacks in the preprocessor.