1 //===--- KernelNameRestrictionCheck.cpp - clang-tidy ----------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "KernelNameRestrictionCheck.h"
10 #include "clang/Frontend/CompilerInstance.h"
11 #include "clang/Lex/PPCallbacks.h"
12 #include "clang/Lex/Preprocessor.h"
16 using namespace clang::ast_matchers
;
24 class KernelNameRestrictionPPCallbacks
: public PPCallbacks
{
26 explicit KernelNameRestrictionPPCallbacks(ClangTidyCheck
&Check
,
27 const SourceManager
&SM
)
28 : Check(Check
), SM(SM
) {}
30 void InclusionDirective(SourceLocation HashLoc
, const Token
&IncludeTok
,
31 StringRef FileName
, bool IsAngled
,
32 CharSourceRange FileNameRange
,
33 Optional
<FileEntryRef
> File
, StringRef SearchPath
,
34 StringRef RelativePath
, const Module
*Imported
,
35 SrcMgr::CharacteristicKind FileType
) override
;
37 void EndOfMainFile() override
;
40 /// Returns true if the name of the file with path FilePath is 'kernel.cl',
41 /// 'verilog.cl', or 'vhdl.cl'. The file name check is case insensitive.
42 bool fileNameIsRestricted(StringRef FilePath
);
44 struct IncludeDirective
{
45 SourceLocation Loc
; // Location in the include directive.
46 StringRef FileName
; // Filename as a string.
49 std::vector
<IncludeDirective
> IncludeDirectives
;
50 ClangTidyCheck
&Check
;
51 const SourceManager
&SM
;
56 void KernelNameRestrictionCheck::registerPPCallbacks(const SourceManager
&SM
,
60 std::make_unique
<KernelNameRestrictionPPCallbacks
>(*this, SM
));
63 void KernelNameRestrictionPPCallbacks::InclusionDirective(
64 SourceLocation HashLoc
, const Token
&, StringRef FileName
, bool,
65 CharSourceRange
, Optional
<FileEntryRef
>, StringRef
, StringRef
,
66 const Module
*, SrcMgr::CharacteristicKind
) {
67 IncludeDirective ID
= {HashLoc
, FileName
};
68 IncludeDirectives
.push_back(std::move(ID
));
71 bool KernelNameRestrictionPPCallbacks::fileNameIsRestricted(
73 return FileName
.equals_insensitive("kernel.cl") ||
74 FileName
.equals_insensitive("verilog.cl") ||
75 FileName
.equals_insensitive("vhdl.cl");
78 void KernelNameRestrictionPPCallbacks::EndOfMainFile() {
80 // Check main file for restricted names.
81 const FileEntry
*Entry
= SM
.getFileEntryForID(SM
.getMainFileID());
82 StringRef FileName
= llvm::sys::path::filename(Entry
->getName());
83 if (fileNameIsRestricted(FileName
))
84 Check
.diag(SM
.getLocForStartOfFile(SM
.getMainFileID()),
85 "compiling '%0' may cause additional compilation errors due "
86 "to the name of the kernel source file; consider renaming the "
87 "included kernel source file")
90 if (IncludeDirectives
.empty())
93 // Check included files for restricted names.
94 for (const IncludeDirective
&ID
: IncludeDirectives
) {
95 StringRef FileName
= llvm::sys::path::filename(ID
.FileName
);
96 if (fileNameIsRestricted(FileName
))
98 "including '%0' may cause additional compilation errors due "
99 "to the name of the kernel source file; consider renaming the "
100 "included kernel source file")
105 } // namespace altera