clang-tools  5.0.0
HeaderGuardCheck.cpp
Go to the documentation of this file.
1 //===--- HeaderGuardCheck.cpp - clang-tidy --------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "HeaderGuardCheck.h"
11 
12 namespace clang {
13 namespace tidy {
14 namespace llvm {
15 
18  : HeaderGuardCheck(Name, Context),
19  RawStringHeaderFileExtensions(
20  Options.getLocalOrGlobal("HeaderFileExtensions", ",h,hh,hpp,hxx")) {
21  utils::parseHeaderFileExtensions(RawStringHeaderFileExtensions,
22  HeaderFileExtensions, ',');
23 }
24 
26  Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions);
27 }
28 
30  return utils::isHeaderFileExtension(FileName, HeaderFileExtensions);
31 }
32 
34  StringRef OldGuard) {
35  std::string Guard = tooling::getAbsolutePath(Filename);
36 
37  // Sanitize the path. There are some rules for compatibility with the historic
38  // style in include/llvm and include/clang which we want to preserve.
39 
40  // We don't want _INCLUDE_ in our guards.
41  size_t PosInclude = Guard.rfind("include/");
42  if (PosInclude != StringRef::npos)
43  Guard = Guard.substr(PosInclude + std::strlen("include/"));
44 
45  // For clang we drop the _TOOLS_.
46  size_t PosToolsClang = Guard.rfind("tools/clang/");
47  if (PosToolsClang != StringRef::npos)
48  Guard = Guard.substr(PosToolsClang + std::strlen("tools/"));
49 
50  // The remainder is LLVM_FULL_PATH_TO_HEADER_H
51  size_t PosLLVM = Guard.rfind("llvm/");
52  if (PosLLVM != StringRef::npos)
53  Guard = Guard.substr(PosLLVM);
54 
55  std::replace(Guard.begin(), Guard.end(), '/', '_');
56  std::replace(Guard.begin(), Guard.end(), '.', '_');
57  std::replace(Guard.begin(), Guard.end(), '-', '_');
58 
59  // The prevalent style in clang is LLVM_CLANG_FOO_BAR_H
60  if (StringRef(Guard).startswith("clang"))
61  Guard = "LLVM_" + Guard;
62 
63  return StringRef(Guard).upper();
64 }
65 
66 } // namespace llvm
67 } // namespace tidy
68 } // namespace clang
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
StringHandle Name
bool parseHeaderFileExtensions(StringRef AllHeaderFileExtensions, HeaderFileExtensionsSet &HeaderFileExtensions, char delimiter)
Parses header file extensions from a semicolon-separated list.
LLVMHeaderGuardCheck(StringRef Name, ClangTidyContext *Context)
std::string getHeaderGuard(StringRef Filename, StringRef OldGuard) override
Gets the canonical header guard for a file.
std::string Filename
Filename as a string.
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Definition: ClangTidy.cpp:449
std::map< std::string, std::string > OptionMap
ClangTidyContext & Context
Definition: ClangTidy.cpp:87
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
bool isHeaderFileExtension(StringRef FileName, const HeaderFileExtensionsSet &HeaderFileExtensions)
Decides whether a file has a header file extension.
bool shouldFixHeaderGuard(StringRef Filename) override
Returns true if the check should suggest changing an existing header guard to the string returned by ...