aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/clang14/tools/extra/clang-tidy/altera/KernelNameRestrictionCheck.cpp
blob: 769494207aef258a6379b9cde4db769845c62616 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//===--- KernelNameRestrictionCheck.cpp - clang-tidy ----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "KernelNameRestrictionCheck.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Preprocessor.h"
#include <string>
#include <vector>

using namespace clang::ast_matchers;

namespace clang {
namespace tidy {
namespace altera {

namespace {

class KernelNameRestrictionPPCallbacks : public PPCallbacks {
public:
  explicit KernelNameRestrictionPPCallbacks(ClangTidyCheck &Check,
                                            const SourceManager &SM)
      : Check(Check), SM(SM) {}

  void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
                          StringRef FileName, bool IsAngled,
                          CharSourceRange FileNameRange, const FileEntry *File,
                          StringRef SearchPath, StringRef RelativePath,
                          const Module *Imported,
                          SrcMgr::CharacteristicKind FileType) override;

  void EndOfMainFile() override;

private:
  /// Returns true if the name of the file with path FilePath is 'kernel.cl',
  /// 'verilog.cl', or 'vhdl.cl'. The file name check is case insensitive.
  bool fileNameIsRestricted(StringRef FilePath);

  struct IncludeDirective {
    SourceLocation Loc; // Location in the include directive.
    StringRef FileName; // Filename as a string.
  };

  std::vector<IncludeDirective> IncludeDirectives;
  ClangTidyCheck &Check;
  const SourceManager &SM;
};

} // namespace

void KernelNameRestrictionCheck::registerPPCallbacks(const SourceManager &SM,
                                                     Preprocessor *PP,
                                                     Preprocessor *) {
  PP->addPPCallbacks(
      std::make_unique<KernelNameRestrictionPPCallbacks>(*this, SM));
}

void KernelNameRestrictionPPCallbacks::InclusionDirective(
    SourceLocation HashLoc, const Token &, StringRef FileName, bool,
    CharSourceRange, const FileEntry *, StringRef, StringRef, const Module *,
    SrcMgr::CharacteristicKind) {
  IncludeDirective ID = {HashLoc, FileName};
  IncludeDirectives.push_back(std::move(ID));
}

bool KernelNameRestrictionPPCallbacks::fileNameIsRestricted(
    StringRef FileName) {
  return FileName.equals_insensitive("kernel.cl") ||
         FileName.equals_insensitive("verilog.cl") ||
         FileName.equals_insensitive("vhdl.cl");
}

void KernelNameRestrictionPPCallbacks::EndOfMainFile() {

  // Check main file for restricted names.
  const FileEntry *Entry = SM.getFileEntryForID(SM.getMainFileID());
  StringRef FileName = llvm::sys::path::filename(Entry->getName());
  if (fileNameIsRestricted(FileName))
    Check.diag(SM.getLocForStartOfFile(SM.getMainFileID()),
               "compiling '%0' may cause additional compilation errors due "
               "to the name of the kernel source file; consider renaming the "
               "included kernel source file")
        << FileName;

  if (IncludeDirectives.empty())
    return;

  // Check included files for restricted names.
  for (const IncludeDirective &ID : IncludeDirectives) {
    StringRef FileName = llvm::sys::path::filename(ID.FileName);
    if (fileNameIsRestricted(FileName))
      Check.diag(ID.Loc,
                 "including '%0' may cause additional compilation errors due "
                 "to the name of the kernel source file; consider renaming the "
                 "included kernel source file")
          << FileName;
  }
}

} // namespace altera
} // namespace tidy
} // namespace clang