diff --git a/clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.h b/clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.h --- 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 { @@ -33,7 +33,7 @@ private: const std::string RawStringHeaderFileExtensions; - utils::HeaderFileExtensionsSet HeaderFileExtensions; + utils::FileExtensionsSet HeaderFileExtensions; }; } // namespace bugprone diff --git a/clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.cpp --- a/clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/DynamicStaticInitializersCheck.cpp @@ -31,8 +31,8 @@ : 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 --- 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 @@ private: const std::string RawStringHeaderFileExtensions; - utils::HeaderFileExtensionsSet HeaderFileExtensions; + utils::FileExtensionsSet HeaderFileExtensions; }; } // namespace readability diff --git a/clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.cpp b/clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.cpp --- a/clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/GlobalNamesInHeadersCheck.cpp @@ -24,8 +24,8 @@ : 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 --- 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 { @@ -38,7 +38,7 @@ private: const std::string RawStringHeaderFileExtensions; - utils::HeaderFileExtensionsSet HeaderFileExtensions; + utils::FileExtensionsSet HeaderFileExtensions; }; } // namespace build diff --git a/clang-tools-extra/clang-tidy/google/UnnamedNamespaceInHeaderCheck.cpp b/clang-tools-extra/clang-tidy/google/UnnamedNamespaceInHeaderCheck.cpp --- a/clang-tools-extra/clang-tidy/google/UnnamedNamespaceInHeaderCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/UnnamedNamespaceInHeaderCheck.cpp @@ -23,8 +23,8 @@ : 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/misc/DefinitionsInHeadersCheck.h b/clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.h --- 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 { @@ -40,7 +40,7 @@ private: const bool UseHeaderFileExtension; const std::string RawStringHeaderFileExtensions; - utils::HeaderFileExtensionsSet HeaderFileExtensions; + utils::FileExtensionsSet HeaderFileExtensions; }; } // namespace misc diff --git a/clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp b/clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp --- a/clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp @@ -18,8 +18,8 @@ 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 @@ 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/utils/CMakeLists.txt b/clang-tools-extra/clang-tidy/utils/CMakeLists.txt --- a/clang-tools-extra/clang-tidy/utils/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/utils/CMakeLists.txt @@ -5,8 +5,8 @@ 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/HeaderFileExtensionsUtils.h b/clang-tools-extra/clang-tidy/utils/FileExtensionsUtils.h rename from clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.h rename to clang-tools-extra/clang-tidy/utils/FileExtensionsUtils.h --- 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. @@ -11,6 +11,7 @@ #include "clang/Basic/SourceLocation.h" #include "clang/Basic/SourceManager.h" +#include "llvm/ADT/Optional.h" #include "llvm/ADT/SmallSet.h" #include "llvm/ADT/StringRef.h" @@ -18,35 +19,31 @@ namespace tidy { namespace utils { -typedef llvm::SmallSet HeaderFileExtensionsSet; +typedef llvm::SmallSet 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/FileExtensionsUtils.cpp b/clang-tools-extra/clang-tidy/utils/FileExtensionsUtils.cpp new file mode 100644 --- /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 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.cpp b/clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.cpp deleted file mode 100644 --- 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 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.h b/clang-tools-extra/clang-tidy/utils/HeaderGuard.h --- 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 @@ : 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 @@ private: std::string RawStringHeaderFileExtensions; - utils::HeaderFileExtensionsSet HeaderFileExtensions; + utils::FileExtensionsSet HeaderFileExtensions; }; } // namespace utils diff --git a/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp b/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp --- a/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp +++ b/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp @@ -273,13 +273,13 @@ } 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) {