Author: Jonathan Roelofs Date: 2020-03-09T11:32:44-06:00 New Revision: 3486cc014b208df3897cf5656db0d0fdeae26d6b
URL: https://github.com/llvm/llvm-project/commit/3486cc014b208df3897cf5656db0d0fdeae26d6b DIFF: https://github.com/llvm/llvm-project/commit/3486cc014b208df3897cf5656db0d0fdeae26d6b.diff LOG: [clang-tidy] Generalize HeaderFileExtensions.{h,cpp}. NFC https://reviews.llvm.org/D75489 Added: clang-tools-extra/clang-tidy/utils/FileExtensionsUtils.cpp clang-tools-extra/clang-tidy/utils/FileExtensionsUtils.h Modified: clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.cpp clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.h clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.cpp clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.h clang-tools-extra/clang-tidy/google/UnnamedNamespaceInHeaderCheck.cpp clang-tools-extra/clang-tidy/google/UnnamedNamespaceInHeaderCheck.h clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.h clang-tools-extra/clang-tidy/utils/CMakeLists.txt clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp clang-tools-extra/clang-tidy/utils/HeaderGuard.h Removed: clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.cpp clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.h ################################################################################ diff --git a/clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.cpp index 1e2184e324fc..7403762122bd 100644 --- a/clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.cpp @@ -31,8 +31,8 @@ DynamicStaticInitializersCheck::DynamicStaticInitializersCheck(StringRef Name, : ClangTidyCheck(Name, Context), RawStringHeaderFileExtensions(Options.getLocalOrGlobal( "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) { - if (!utils::parseHeaderFileExtensions(RawStringHeaderFileExtensions, - HeaderFileExtensions, ',')) { + if (!utils::parseFileExtensions(RawStringHeaderFileExtensions, + HeaderFileExtensions, ',')) { llvm::errs() << "Invalid header file extension: " << RawStringHeaderFileExtensions << "\n"; } diff --git a/clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.h b/clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.h index 1cf6e408aa42..ac0d32202d61 100644 --- a/clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.h +++ b/clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.h @@ -10,7 +10,7 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_DYNAMIC_STATIC_INITIALIZERS_CHECK_H #include "../ClangTidyCheck.h" -#include "../utils/HeaderFileExtensionsUtils.h" +#include "../utils/FileExtensionsUtils.h" namespace clang { namespace tidy { @@ -36,7 +36,7 @@ class DynamicStaticInitializersCheck : public ClangTidyCheck { private: const std::string RawStringHeaderFileExtensions; - utils::HeaderFileExtensionsSet HeaderFileExtensions; + utils::FileExtensionsSet HeaderFileExtensions; }; } // namespace bugprone diff --git a/clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.cpp b/clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.cpp index c6d72494243c..09eb266efb35 100644 --- a/clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.cpp @@ -24,8 +24,8 @@ GlobalNamesInHeadersCheck::GlobalNamesInHeadersCheck(StringRef Name, : ClangTidyCheck(Name, Context), RawStringHeaderFileExtensions(Options.getLocalOrGlobal( "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) { - if (!utils::parseHeaderFileExtensions(RawStringHeaderFileExtensions, - HeaderFileExtensions, ',')) { + if (!utils::parseFileExtensions(RawStringHeaderFileExtensions, + HeaderFileExtensions, ',')) { llvm::errs() << "Invalid header file extension: " << RawStringHeaderFileExtensions << "\n"; } diff --git a/clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.h b/clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.h index 730ef60c70fc..cd69df72962e 100644 --- a/clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.h +++ b/clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.h @@ -10,7 +10,7 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_GLOBALNAMESINHEADERSCHECK_H #include "../ClangTidyCheck.h" -#include "../utils/HeaderFileExtensionsUtils.h" +#include "../utils/FileExtensionsUtils.h" namespace clang { namespace tidy { @@ -35,7 +35,7 @@ class GlobalNamesInHeadersCheck : public ClangTidyCheck { private: const std::string RawStringHeaderFileExtensions; - utils::HeaderFileExtensionsSet HeaderFileExtensions; + utils::FileExtensionsSet HeaderFileExtensions; }; } // namespace readability diff --git a/clang-tools-extra/clang-tidy/google/UnnamedNamespaceInHeaderCheck.cpp b/clang-tools-extra/clang-tidy/google/UnnamedNamespaceInHeaderCheck.cpp index c14427a2b139..1c277d7f665b 100644 --- a/clang-tools-extra/clang-tidy/google/UnnamedNamespaceInHeaderCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/UnnamedNamespaceInHeaderCheck.cpp @@ -23,8 +23,8 @@ UnnamedNamespaceInHeaderCheck::UnnamedNamespaceInHeaderCheck( : ClangTidyCheck(Name, Context), RawStringHeaderFileExtensions(Options.getLocalOrGlobal( "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) { - if (!utils::parseHeaderFileExtensions(RawStringHeaderFileExtensions, - HeaderFileExtensions, ',')) { + if (!utils::parseFileExtensions(RawStringHeaderFileExtensions, + HeaderFileExtensions, ',')) { llvm::errs() << "Invalid header file extension: " << RawStringHeaderFileExtensions << "\n"; } diff --git a/clang-tools-extra/clang-tidy/google/UnnamedNamespaceInHeaderCheck.h b/clang-tools-extra/clang-tidy/google/UnnamedNamespaceInHeaderCheck.h index fe011119f0eb..025637d08d4e 100644 --- a/clang-tools-extra/clang-tidy/google/UnnamedNamespaceInHeaderCheck.h +++ b/clang-tools-extra/clang-tidy/google/UnnamedNamespaceInHeaderCheck.h @@ -10,7 +10,7 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GOOGLE_UNNAMEDNAMESPACEINHEADERCHECK_H #include "../ClangTidyCheck.h" -#include "../utils/HeaderFileExtensionsUtils.h" +#include "../utils/FileExtensionsUtils.h" namespace clang { namespace tidy { @@ -41,7 +41,7 @@ class UnnamedNamespaceInHeaderCheck : public ClangTidyCheck { private: const std::string RawStringHeaderFileExtensions; - utils::HeaderFileExtensionsSet HeaderFileExtensions; + utils::FileExtensionsSet HeaderFileExtensions; }; } // namespace build diff --git a/clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp b/clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp index 32b75c365453..6d743bf51765 100644 --- a/clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp @@ -18,8 +18,8 @@ namespace misc { namespace { -AST_MATCHER_P(NamedDecl, usesHeaderFileExtension, - utils::HeaderFileExtensionsSet, HeaderFileExtensions) { +AST_MATCHER_P(NamedDecl, usesHeaderFileExtension, utils::FileExtensionsSet, + HeaderFileExtensions) { return utils::isExpansionLocInHeaderFile( Node.getBeginLoc(), Finder->getASTContext().getSourceManager(), HeaderFileExtensions); @@ -33,8 +33,8 @@ DefinitionsInHeadersCheck::DefinitionsInHeadersCheck(StringRef Name, UseHeaderFileExtension(Options.get("UseHeaderFileExtension", true)), RawStringHeaderFileExtensions(Options.getLocalOrGlobal( "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) { - if (!utils::parseHeaderFileExtensions(RawStringHeaderFileExtensions, - HeaderFileExtensions, ',')) { + if (!utils::parseFileExtensions(RawStringHeaderFileExtensions, + HeaderFileExtensions, ',')) { // FIXME: Find a more suitable way to handle invalid configuration // options. llvm::errs() << "Invalid header file extension: " diff --git a/clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.h b/clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.h index 7ba8a128f8bc..48abe799bc84 100644 --- a/clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.h +++ b/clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.h @@ -10,7 +10,7 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_DEFINITIONS_IN_HEADERS_H #include "../ClangTidyCheck.h" -#include "../utils/HeaderFileExtensionsUtils.h" +#include "../utils/FileExtensionsUtils.h" namespace clang { namespace tidy { @@ -43,7 +43,7 @@ class DefinitionsInHeadersCheck : public ClangTidyCheck { private: const bool UseHeaderFileExtension; const std::string RawStringHeaderFileExtensions; - utils::HeaderFileExtensionsSet HeaderFileExtensions; + utils::FileExtensionsSet HeaderFileExtensions; }; } // namespace misc diff --git a/clang-tools-extra/clang-tidy/utils/CMakeLists.txt b/clang-tools-extra/clang-tidy/utils/CMakeLists.txt index 885c83798a04..97171fb758a8 100644 --- a/clang-tools-extra/clang-tidy/utils/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/utils/CMakeLists.txt @@ -5,8 +5,8 @@ add_clang_library(clangTidyUtils DeclRefExprUtils.cpp ExceptionAnalyzer.cpp ExprSequence.cpp + FileExtensionsUtils.cpp FixItHintUtils.cpp - HeaderFileExtensionsUtils.cpp HeaderGuard.cpp IncludeInserter.cpp IncludeSorter.cpp diff --git a/clang-tools-extra/clang-tidy/utils/FileExtensionsUtils.cpp b/clang-tools-extra/clang-tidy/utils/FileExtensionsUtils.cpp new file mode 100644 index 000000000000..c016a72362c5 --- /dev/null +++ b/clang-tools-extra/clang-tidy/utils/FileExtensionsUtils.cpp @@ -0,0 +1,60 @@ +//===--- FileExtensionsUtils.cpp - 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 +// +//===----------------------------------------------------------------------===// + +#include "FileExtensionsUtils.h" +#include "clang/Basic/CharInfo.h" +#include "llvm/Support/Path.h" + +namespace clang { +namespace tidy { +namespace utils { + +bool isExpansionLocInHeaderFile(SourceLocation Loc, const SourceManager &SM, + const FileExtensionsSet &HeaderFileExtensions) { + SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc); + return isFileExtension(SM.getFilename(ExpansionLoc), HeaderFileExtensions); +} + +bool isPresumedLocInHeaderFile(SourceLocation Loc, SourceManager &SM, + const FileExtensionsSet &HeaderFileExtensions) { + PresumedLoc PresumedLocation = SM.getPresumedLoc(Loc); + return isFileExtension(PresumedLocation.getFilename(), HeaderFileExtensions); +} + +bool isSpellingLocInHeaderFile(SourceLocation Loc, SourceManager &SM, + const FileExtensionsSet &HeaderFileExtensions) { + SourceLocation SpellingLoc = SM.getSpellingLoc(Loc); + return isFileExtension(SM.getFilename(SpellingLoc), HeaderFileExtensions); +} + +bool parseFileExtensions(StringRef AllFileExtensions, + FileExtensionsSet &FileExtensions, char Delimiter) { + SmallVector<StringRef, 5> Suffixes; + AllFileExtensions.split(Suffixes, Delimiter); + FileExtensions.clear(); + for (StringRef Suffix : Suffixes) { + StringRef Extension = Suffix.trim(); + if (!llvm::all_of(Extension, isAlphanumeric)) + return false; + FileExtensions.insert(Extension); + } + return true; +} + +bool isFileExtension(StringRef FileName, + const FileExtensionsSet &FileExtensions) { + StringRef Extension = llvm::sys::path::extension(FileName); + if (Extension.empty()) + return false; + // Skip "." prefix. + return FileExtensions.count(Extension.substr(1)) > 0; +} + +} // namespace utils +} // namespace tidy +} // namespace clang diff --git a/clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.h b/clang-tools-extra/clang-tidy/utils/FileExtensionsUtils.h similarity index 56% rename from clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.h rename to clang-tools-extra/clang-tidy/utils/FileExtensionsUtils.h index 48265c7dbe75..f83d357946c4 100644 --- a/clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.h +++ b/clang-tools-extra/clang-tidy/utils/FileExtensionsUtils.h @@ -1,4 +1,4 @@ -//===--- HeaderFileExtensionsUtils.h - clang-tidy----------------*- C++ -*-===// +//===--- FileExtensionsUtils.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. @@ -18,35 +18,31 @@ namespace clang { namespace tidy { namespace utils { -typedef llvm::SmallSet<llvm::StringRef, 5> HeaderFileExtensionsSet; +typedef llvm::SmallSet<llvm::StringRef, 5> FileExtensionsSet; /// Checks whether expansion location of \p Loc is in header file. -bool isExpansionLocInHeaderFile( - SourceLocation Loc, const SourceManager &SM, - const HeaderFileExtensionsSet &HeaderFileExtensions); +bool isExpansionLocInHeaderFile(SourceLocation Loc, const SourceManager &SM, + const FileExtensionsSet &HeaderFileExtensions); /// Checks whether presumed location of \p Loc is in header file. -bool isPresumedLocInHeaderFile( - SourceLocation Loc, SourceManager &SM, - const HeaderFileExtensionsSet &HeaderFileExtensions); +bool isPresumedLocInHeaderFile(SourceLocation Loc, SourceManager &SM, + const FileExtensionsSet &HeaderFileExtensions); /// Checks whether spelling location of \p Loc is in header file. -bool isSpellingLocInHeaderFile( - SourceLocation Loc, SourceManager &SM, - const HeaderFileExtensionsSet &HeaderFileExtensions); +bool isSpellingLocInHeaderFile(SourceLocation Loc, SourceManager &SM, + const FileExtensionsSet &HeaderFileExtensions); /// Returns recommended default value for the list of header file /// extensions. inline StringRef defaultHeaderFileExtensions() { return ",h,hh,hpp,hxx"; } /// Parses header file extensions from a semicolon-separated list. -bool parseHeaderFileExtensions(StringRef AllHeaderFileExtensions, - HeaderFileExtensionsSet &HeaderFileExtensions, - char delimiter); +bool parseFileExtensions(StringRef AllFileExtensions, + FileExtensionsSet &FileExtensions, char Delimiter); -/// Decides whether a file has a header file extension. -bool isHeaderFileExtension(StringRef FileName, - const HeaderFileExtensionsSet &HeaderFileExtensions); +/// Decides whether a file has one of the specified file extensions. +bool isFileExtension(StringRef FileName, + const FileExtensionsSet &FileExtensions); } // namespace utils } // namespace tidy diff --git a/clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.cpp b/clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.cpp deleted file mode 100644 index 0215b2f47195..000000000000 --- a/clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.cpp +++ /dev/null @@ -1,70 +0,0 @@ -//===--- HeaderFileExtensionsUtils.cpp - 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 -// -//===----------------------------------------------------------------------===// - -#include "HeaderFileExtensionsUtils.h" -#include "clang/Basic/CharInfo.h" -#include "llvm/Support/Path.h" - -namespace clang { -namespace tidy { -namespace utils { - -bool isExpansionLocInHeaderFile( - SourceLocation Loc, const SourceManager &SM, - const HeaderFileExtensionsSet &HeaderFileExtensions) { - SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc); - return isHeaderFileExtension(SM.getFilename(ExpansionLoc), - HeaderFileExtensions); -} - -bool isPresumedLocInHeaderFile( - SourceLocation Loc, SourceManager &SM, - const HeaderFileExtensionsSet &HeaderFileExtensions) { - PresumedLoc PresumedLocation = SM.getPresumedLoc(Loc); - return isHeaderFileExtension(PresumedLocation.getFilename(), - HeaderFileExtensions); -} - -bool isSpellingLocInHeaderFile( - SourceLocation Loc, SourceManager &SM, - const HeaderFileExtensionsSet &HeaderFileExtensions) { - SourceLocation SpellingLoc = SM.getSpellingLoc(Loc); - return isHeaderFileExtension(SM.getFilename(SpellingLoc), - HeaderFileExtensions); -} - -bool parseHeaderFileExtensions(StringRef AllHeaderFileExtensions, - HeaderFileExtensionsSet &HeaderFileExtensions, - char delimiter) { - SmallVector<StringRef, 5> Suffixes; - AllHeaderFileExtensions.split(Suffixes, delimiter); - HeaderFileExtensions.clear(); - for (StringRef Suffix : Suffixes) { - StringRef Extension = Suffix.trim(); - for (StringRef::const_iterator it = Extension.begin(); - it != Extension.end(); ++it) { - if (!isAlphanumeric(*it)) - return false; - } - HeaderFileExtensions.insert(Extension); - } - return true; -} - -bool isHeaderFileExtension( - StringRef FileName, const HeaderFileExtensionsSet &HeaderFileExtensions) { - StringRef extension = llvm::sys::path::extension(FileName); - if (extension.empty()) - return false; - // Skip "." prefix. - return HeaderFileExtensions.count(extension.substr(1)) > 0; -} - -} // namespace utils -} // namespace tidy -} // namespace clang diff --git a/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp b/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp index 13107c25b60b..1426fca808b1 100644 --- a/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp +++ b/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp @@ -273,13 +273,13 @@ void HeaderGuardCheck::registerPPCallbacks(const SourceManager &SM, } bool HeaderGuardCheck::shouldSuggestEndifComment(StringRef FileName) { - return utils::isHeaderFileExtension(FileName, HeaderFileExtensions); + return utils::isFileExtension(FileName, HeaderFileExtensions); } bool HeaderGuardCheck::shouldFixHeaderGuard(StringRef FileName) { return true; } bool HeaderGuardCheck::shouldSuggestToAddHeaderGuard(StringRef FileName) { - return utils::isHeaderFileExtension(FileName, HeaderFileExtensions); + return utils::isFileExtension(FileName, HeaderFileExtensions); } std::string HeaderGuardCheck::formatEndIf(StringRef HeaderGuard) { diff --git a/clang-tools-extra/clang-tidy/utils/HeaderGuard.h b/clang-tools-extra/clang-tidy/utils/HeaderGuard.h index 8ece3311bebd..cc3cafdeece9 100644 --- a/clang-tools-extra/clang-tidy/utils/HeaderGuard.h +++ b/clang-tools-extra/clang-tidy/utils/HeaderGuard.h @@ -10,7 +10,7 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADERGUARD_H #include "../ClangTidy.h" -#include "../utils/HeaderFileExtensionsUtils.h" +#include "../utils/FileExtensionsUtils.h" namespace clang { namespace tidy { @@ -29,8 +29,8 @@ class HeaderGuardCheck : public ClangTidyCheck { : ClangTidyCheck(Name, Context), RawStringHeaderFileExtensions(Options.getLocalOrGlobal( "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) { - utils::parseHeaderFileExtensions(RawStringHeaderFileExtensions, - HeaderFileExtensions, ','); + utils::parseFileExtensions(RawStringHeaderFileExtensions, + HeaderFileExtensions, ','); } void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override; @@ -54,7 +54,7 @@ class HeaderGuardCheck : public ClangTidyCheck { private: std::string RawStringHeaderFileExtensions; - utils::HeaderFileExtensionsSet HeaderFileExtensions; + utils::FileExtensionsSet HeaderFileExtensions; }; } // namespace utils _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits