[clang-tools-extra] r279803 - [clang-tidy] Added hh, hxx and hpp to header guard checks.
Mads Ravn via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 25 22:59:54 PDT 2016
Author: madsravn
Date: Fri Aug 26 00:59:53 2016
New Revision: 279803
URL: http://llvm.org/viewvc/llvm-project?rev=279803&view=rev
Log:
[clang-tidy] Added hh, hxx and hpp to header guard checks.
Changed the extension check to include the option of ",h,hh,hpp,hxx" instead of just returning whether the file ended with ".h".
Differential revision: https://reviews.llvm.org/D20512
Modified:
clang-tools-extra/trunk/clang-tidy/llvm/HeaderGuardCheck.cpp
clang-tools-extra/trunk/clang-tidy/llvm/HeaderGuardCheck.h
clang-tools-extra/trunk/clang-tidy/utils/HeaderFileExtensionsUtils.cpp
clang-tools-extra/trunk/clang-tidy/utils/HeaderFileExtensionsUtils.h
clang-tools-extra/trunk/clang-tidy/utils/HeaderGuard.cpp
clang-tools-extra/trunk/clang-tidy/utils/HeaderGuard.h
clang-tools-extra/trunk/docs/clang-tidy/checks/llvm-header-guard.rst
Modified: clang-tools-extra/trunk/clang-tidy/llvm/HeaderGuardCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/llvm/HeaderGuardCheck.cpp?rev=279803&r1=279802&r2=279803&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/llvm/HeaderGuardCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/llvm/HeaderGuardCheck.cpp Fri Aug 26 00:59:53 2016
@@ -13,8 +13,8 @@ namespace clang {
namespace tidy {
namespace llvm {
-bool LLVMHeaderGuardCheck::shouldFixHeaderGuard(StringRef Filename) {
- return Filename.endswith(".h");
+bool LLVMHeaderGuardCheck::shouldFixHeaderGuard(StringRef FileName) {
+ return utils::isHeaderFileExtension(FileName, HeaderFileExtensions);
}
std::string LLVMHeaderGuardCheck::getHeaderGuard(StringRef Filename,
Modified: clang-tools-extra/trunk/clang-tidy/llvm/HeaderGuardCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/llvm/HeaderGuardCheck.h?rev=279803&r1=279802&r2=279803&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/llvm/HeaderGuardCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/llvm/HeaderGuardCheck.h Fri Aug 26 00:59:53 2016
@@ -17,13 +17,30 @@ namespace tidy {
namespace llvm {
/// Finds and fixes header guards that do not adhere to LLVM style.
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/llvm-header-guard.html
+/// The check supports these options:
+/// - `HeaderFileExtensions`: a comma-separated list of filename extensions of
+/// header files (The filename extension should not contain "." prefix).
+/// ",h,hh,hpp,hxx" by default.
+/// For extension-less header files, using an empty string or leaving an
+/// empty string between "," if there are other filename extensions.
class LLVMHeaderGuardCheck : public utils::HeaderGuardCheck {
public:
LLVMHeaderGuardCheck(StringRef Name, ClangTidyContext *Context)
- : HeaderGuardCheck(Name, Context) {}
+ : HeaderGuardCheck(Name, Context),
+ RawStringHeaderFileExtensions(
+ Options.getLocalOrGlobal("HeaderFileExtensions", ",h,hh,hpp,hxx")) {
+ utils::parseHeaderFileExtensions(RawStringHeaderFileExtensions,
+ HeaderFileExtensions, ',');
+ }
bool shouldSuggestEndifComment(StringRef Filename) override { return false; }
bool shouldFixHeaderGuard(StringRef Filename) override;
std::string getHeaderGuard(StringRef Filename, StringRef OldGuard) override;
+
+private:
+ std::string RawStringHeaderFileExtensions;
+ utils::HeaderFileExtensionsSet HeaderFileExtensions;
};
} // namespace llvm
Modified: clang-tools-extra/trunk/clang-tidy/utils/HeaderFileExtensionsUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/utils/HeaderFileExtensionsUtils.cpp?rev=279803&r1=279802&r2=279803&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/utils/HeaderFileExtensionsUtils.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/utils/HeaderFileExtensionsUtils.cpp Fri Aug 26 00:59:53 2016
@@ -61,6 +61,15 @@ bool parseHeaderFileExtensions(StringRef
return true;
}
+bool isHeaderFileExtension(StringRef FileName,
+ HeaderFileExtensionsSet HeaderFileExtensions) {
+ StringRef extension = ::llvm::sys::path::extension(FileName);
+ if (extension.startswith("."))
+ extension = extension.substr(1);
+
+ return HeaderFileExtensions.count(extension) > 0;
+}
+
} // namespace utils
} // namespace tidy
} // namespace clang
Modified: clang-tools-extra/trunk/clang-tidy/utils/HeaderFileExtensionsUtils.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/utils/HeaderFileExtensionsUtils.h?rev=279803&r1=279802&r2=279803&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/utils/HeaderFileExtensionsUtils.h (original)
+++ clang-tools-extra/trunk/clang-tidy/utils/HeaderFileExtensionsUtils.h Fri Aug 26 00:59:53 2016
@@ -12,8 +12,8 @@
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
-#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/SmallSet.h"
+#include "llvm/ADT/StringRef.h"
namespace clang {
namespace tidy {
@@ -41,6 +41,10 @@ bool parseHeaderFileExtensions(StringRef
HeaderFileExtensionsSet &HeaderFileExtensions,
char delimiter);
+/// \brief Decides whether a file has a header file extension.
+bool isHeaderFileExtension(StringRef FileName,
+ HeaderFileExtensionsSet HeaderFileExtensions);
+
} // namespace utils
} // namespace tidy
} // namespace clang
Modified: clang-tools-extra/trunk/clang-tidy/utils/HeaderGuard.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/utils/HeaderGuard.cpp?rev=279803&r1=279802&r2=279803&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/utils/HeaderGuard.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/utils/HeaderGuard.cpp Fri Aug 26 00:59:53 2016
@@ -20,7 +20,7 @@ namespace utils {
/// \brief canonicalize a path by removing ./ and ../ components.
static std::string cleanPath(StringRef Path) {
- SmallString<256> Result = Path;
+ SmallString<256> Result = Path;
llvm::sys::path::remove_dots(Result, true);
return Result.str();
}
@@ -274,13 +274,13 @@ void HeaderGuardCheck::registerPPCallbac
}
bool HeaderGuardCheck::shouldSuggestEndifComment(StringRef FileName) {
- return FileName.endswith(".h");
+ return utils::isHeaderFileExtension(FileName, HeaderFileExtensions);
}
bool HeaderGuardCheck::shouldFixHeaderGuard(StringRef FileName) { return true; }
bool HeaderGuardCheck::shouldSuggestToAddHeaderGuard(StringRef FileName) {
- return FileName.endswith(".h");
+ return utils::isHeaderFileExtension(FileName, HeaderFileExtensions);
}
std::string HeaderGuardCheck::formatEndIf(StringRef HeaderGuard) {
Modified: clang-tools-extra/trunk/clang-tidy/utils/HeaderGuard.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/utils/HeaderGuard.h?rev=279803&r1=279802&r2=279803&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/utils/HeaderGuard.h (original)
+++ clang-tools-extra/trunk/clang-tidy/utils/HeaderGuard.h Fri Aug 26 00:59:53 2016
@@ -11,16 +11,28 @@
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H
#include "../ClangTidy.h"
+#include "../utils/HeaderFileExtensionsUtils.h"
namespace clang {
namespace tidy {
namespace utils {
/// Finds and fixes header guards.
+/// The check supports these options:
+/// - `HeaderFileExtensions`: a comma-separated list of filename extensions of
+/// header files (The filename extension should not contain "." prefix).
+/// ",h,hh,hpp,hxx" by default.
+/// For extension-less header files, using an empty string or leaving an
+/// empty string between "," if there are other filename extensions.
class HeaderGuardCheck : public ClangTidyCheck {
public:
HeaderGuardCheck(StringRef Name, ClangTidyContext *Context)
- : ClangTidyCheck(Name, Context) {}
+ : ClangTidyCheck(Name, Context),
+ RawStringHeaderFileExtensions(
+ Options.getLocalOrGlobal("HeaderFileExtensions", ",h,hh,hpp,hxx")) {
+ utils::parseHeaderFileExtensions(RawStringHeaderFileExtensions,
+ HeaderFileExtensions, ',');
+ }
void registerPPCallbacks(CompilerInstance &Compiler) override;
/// Returns ``true`` if the check should suggest inserting a trailing comment
@@ -39,6 +51,10 @@ public:
/// Gets the canonical header guard for a file.
virtual std::string getHeaderGuard(StringRef Filename,
StringRef OldGuard = StringRef()) = 0;
+
+private:
+ std::string RawStringHeaderFileExtensions;
+ utils::HeaderFileExtensionsSet HeaderFileExtensions;
};
} // namespace utils
Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/llvm-header-guard.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/llvm-header-guard.rst?rev=279803&r1=279802&r2=279803&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/llvm-header-guard.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/llvm-header-guard.rst Fri Aug 26 00:59:53 2016
@@ -3,4 +3,11 @@
llvm-header-guard
=================
-TODO: add docs
+Finds and fixes header guards that do not adhere to LLVM style.
+
+Options
+-------
+
+.. option:: HeaderFileExtensions
+
+ A comma-separated list of filename extensions of header files (The filename extension should not contain "." prefix). Default value is ",h,hh,hpp,hxx". For extension-less header files, using an empty string or leaving an empty string between "," if there are other filename extensions.
More information about the cfe-commits
mailing list