Index: clang-tidy/bugprone/BugproneTidyModule.cpp =================================================================== --- clang-tidy/bugprone/BugproneTidyModule.cpp +++ clang-tidy/bugprone/BugproneTidyModule.cpp @@ -19,6 +19,7 @@ #include "FoldInitTypeCheck.h" #include "ForwardDeclarationNamespaceCheck.h" #include "ForwardingReferenceOverloadCheck.h" +#include "HeaderGuardCheck.h" #include "InaccurateEraseCheck.h" #include "IncorrectRoundingsCheck.h" #include "IntegerDivisionCheck.h" @@ -76,6 +77,8 @@ "bugprone-forward-declaration-namespace"); CheckFactories.registerCheck( "bugprone-forwarding-reference-overload"); + CheckFactories.registerCheck( + "bugprone-header-guard"); CheckFactories.registerCheck( "bugprone-inaccurate-erase"); CheckFactories.registerCheck( Index: clang-tidy/bugprone/CMakeLists.txt =================================================================== --- clang-tidy/bugprone/CMakeLists.txt +++ clang-tidy/bugprone/CMakeLists.txt @@ -11,6 +11,7 @@ FoldInitTypeCheck.cpp ForwardDeclarationNamespaceCheck.cpp ForwardingReferenceOverloadCheck.cpp + HeaderGuardCheck.cpp InaccurateEraseCheck.cpp IncorrectRoundingsCheck.cpp IntegerDivisionCheck.cpp Index: clang-tidy/bugprone/HeaderGuardCheck.h =================================================================== --- /dev/null +++ clang-tidy/bugprone/HeaderGuardCheck.h @@ -0,0 +1,33 @@ +//===--- HeaderGuardCheck.h - clang-tidy ------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_HEADERGUARDCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_HEADERGUARDCHECK_H + +#include "../utils/HeaderGuard.h" + +namespace clang { +namespace tidy { +namespace bugprone { + +/// Finds and fixes missing header guards and does not enforce any style. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-header-guard.html +class BugproneHeaderGuardCheck : public utils::HeaderGuardCheck { +public: + BugproneHeaderGuardCheck(StringRef Name, ClangTidyContext *Context); + bool shouldSuggestEndifComment(StringRef Filename) override { return false; } + std::string getHeaderGuard(StringRef Filename, StringRef OldGuard) override; +}; + +} // namespace bugprone +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_HEADERGUARDCHECK_H Index: clang-tidy/bugprone/HeaderGuardCheck.cpp =================================================================== --- /dev/null +++ clang-tidy/bugprone/HeaderGuardCheck.cpp @@ -0,0 +1,32 @@ +//===--- HeaderGuardCheck.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 "HeaderGuardCheck.h" + +namespace clang { +namespace tidy { +namespace bugprone { + +BugproneHeaderGuardCheck::BugproneHeaderGuardCheck(StringRef Name, + ClangTidyContext *Context) + : HeaderGuardCheck(Name, Context) {} + +std::string BugproneHeaderGuardCheck::getHeaderGuard(StringRef Filename, + StringRef OldGuard) { + if (OldGuard.size()) + return OldGuard; + + std::string Guard = llvm::sys::path::filename(Filename); + std::replace(Guard.begin(), Guard.end(), '.', '_'); + std::replace(Guard.begin(), Guard.end(), '-', '_'); + return StringRef(Guard).upper(); +} + +} // namespace bugprone +} // namespace tidy +} // namespace clang Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -108,6 +108,11 @@ Checks whether there are underscores in googletest test and test case names in test macros, which is prohibited by the Googletest FAQ. +- New :doc:`bugprone-header-guard + ` check. + + Finds and fixes missing header guards and does not enforce any style. + - New :doc:`objc-super-self ` check. Finds invocations of ``-self`` on super instances in initializers of Index: docs/clang-tidy/checks/bugprone-header-guard.rst =================================================================== --- /dev/null +++ docs/clang-tidy/checks/bugprone-header-guard.rst @@ -0,0 +1,18 @@ +.. title:: clang-tidy - bugprone-header-guard + +bugprone-header-guard +===================== + +Finds and fixes missing header guards and does not enforce any style. + +Options +------- + +.. option:: HeaderFileExtensions + + A comma-separated list of filename extensions of header files (the filename + extensions should not include "." prefix). Default is `h,hh,hpp,hxx`. + For header files without an extension, use an empty string (if there are no + other desired extensions) or leave an empty element in the list. e.g., + `h,hh,hpp,hxx,` (note the trailing comma). + Index: docs/clang-tidy/checks/google-build-namespaces.rst =================================================================== --- docs/clang-tidy/checks/google-build-namespaces.rst +++ docs/clang-tidy/checks/google-build-namespaces.rst @@ -18,7 +18,7 @@ .. option:: HeaderFileExtensions A comma-separated list of filename extensions of header files (the filename - extensions should not include "." prefix). Default is "h,hh,hpp,hxx". + extensions should not include "." prefix). Default is `h,hh,hpp,hxx`. For header files without an extension, use an empty string (if there are no other desired extensions) or leave an empty element in the list. e.g., - "h,hh,hpp,hxx," (note the trailing comma). + `h,hh,hpp,hxx,` (note the trailing comma). Index: docs/clang-tidy/checks/google-global-names-in-headers.rst =================================================================== --- docs/clang-tidy/checks/google-global-names-in-headers.rst +++ docs/clang-tidy/checks/google-global-names-in-headers.rst @@ -15,7 +15,7 @@ .. option:: HeaderFileExtensions A comma-separated list of filename extensions of header files (the filename - extensions should not contain "." prefix). Default is "h". + extensions should not include "." prefix). Default is `h,hh,hpp,hxx`. For header files without an extension, use an empty string (if there are no other desired extensions) or leave an empty element in the list. e.g., - "h,hh,hpp,hxx," (note the trailing comma). + `h,hh,hpp,hxx,` (note the trailing comma). Index: docs/clang-tidy/checks/list.rst =================================================================== --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -44,6 +44,7 @@ bugprone-fold-init-type bugprone-forward-declaration-namespace bugprone-forwarding-reference-overload + bugprone-header-guard bugprone-inaccurate-erase bugprone-incorrect-roundings bugprone-integer-division Index: docs/clang-tidy/checks/llvm-header-guard.rst =================================================================== --- docs/clang-tidy/checks/llvm-header-guard.rst +++ docs/clang-tidy/checks/llvm-header-guard.rst @@ -11,7 +11,7 @@ .. option:: HeaderFileExtensions A comma-separated list of filename extensions of header files (the filename - extensions should not include "." prefix). Default is "h,hh,hpp,hxx". + extensions should not include "." prefix). Default is `h,hh,hpp,hxx`. For header files without an extension, use an empty string (if there are no other desired extensions) or leave an empty element in the list. e.g., - "h,hh,hpp,hxx," (note the trailing comma). + `h,hh,hpp,hxx,` (note the trailing comma). Index: docs/clang-tidy/checks/misc-definitions-in-headers.rst =================================================================== --- docs/clang-tidy/checks/misc-definitions-in-headers.rst +++ docs/clang-tidy/checks/misc-definitions-in-headers.rst @@ -89,10 +89,10 @@ .. option:: HeaderFileExtensions A comma-separated list of filename extensions of header files (the filename - extensions should not include "." prefix). Default is "h,hh,hpp,hxx". + extensions should not include "." prefix). Default is `h,hh,hpp,hxx`. For header files without an extension, use an empty string (if there are no other desired extensions) or leave an empty element in the list. e.g., - "h,hh,hpp,hxx," (note the trailing comma). + `h,hh,hpp,hxx,` (note the trailing comma). .. option:: UseHeaderFileExtension Index: test/clang-tidy/bugprone-header-guard.cpp =================================================================== --- /dev/null +++ test/clang-tidy/bugprone-header-guard.cpp @@ -0,0 +1,9 @@ +// RUN: %check_clang_tidy %s bugprone-header-guard %t -- \ +// RUN: -config="{CheckOptions: [{key: bugprone-header-guard.HeaderFileExtensions, value: 'cpp'}]}" \ +// RUN: -header-filter=.* -- + +// CHECK-MESSAGES: 1:1: warning: header is missing header guard + +// CHECK-FIXES: #ifndef BUGPRONE_HEADER_GUARD_CPP_TMP_CPP +// CHECK-FIXES-NEXT: #define BUGPRONE_HEADER_GUARD_CPP_TMP_CPP +// CHECK-FIXES: #endif