diff --git a/ClangTidyCheck.h b/ClangTidyCheck.h index abf528d..17e9df9 100644 --- a/ClangTidyCheck.h +++ b/ClangTidyCheck.h @@ -13,7 +13,7 @@ #include "ClangTidyOptions.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Basic/Diagnostic.h" -#include "llvm/ADT/Optional.h" +#include #include #include #include @@ -154,8 +154,8 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback { /// /// Reads the option with the check-local name \p LocalName from the /// ``CheckOptions``. If the corresponding key is not present, return - /// ``None``. - llvm::Optional get(StringRef LocalName) const; + /// ``std::nullopt``. + std::optional get(StringRef LocalName) const; /// Read a named option from the ``Context``. /// @@ -169,8 +169,8 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback { /// Reads the option with the check-local name \p LocalName from local or /// global ``CheckOptions``. Gets local option first. If local is not /// present, falls back to get global option. If global option is not - /// present either, return ``None``. - llvm::Optional getLocalOrGlobal(StringRef LocalName) const; + /// present either, return ``std::nullopt``. + std::optional getLocalOrGlobal(StringRef LocalName) const; /// Read a named option from the ``Context``. /// @@ -185,20 +185,20 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback { /// /// Reads the option with the check-local name \p LocalName from the /// ``CheckOptions``. If the corresponding key is not present, return - /// ``None``. + /// ``std::nullopt``. /// /// If the corresponding key can't be parsed as a ``T``, emit a - /// diagnostic and return ``None``. + /// diagnostic and return ``std::nullopt``. template - std::enable_if_t::value, llvm::Optional> + std::enable_if_t::value, std::optional> get(StringRef LocalName) const { - if (llvm::Optional Value = get(LocalName)) { + if (std::optional Value = get(LocalName)) { T Result{}; if (!StringRef(*Value).getAsInteger(10, Result)) return Result; diagnoseBadIntegerOption(NamePrefix + LocalName, *Value); } - return None; + return std::nullopt; } /// Read a named option from the ``Context`` and parse it as an @@ -222,27 +222,27 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback { /// Reads the option with the check-local name \p LocalName from local or /// global ``CheckOptions``. Gets local option first. If local is not /// present, falls back to get global option. If global option is not - /// present either, return ``None``. + /// present either, return ``std::nullopt``. /// /// If the corresponding key can't be parsed as a ``T``, emit a - /// diagnostic and return ``None``. + /// diagnostic and return ``std::nullopt``. template - std::enable_if_t::value, llvm::Optional> + std::enable_if_t::value, std::optional> getLocalOrGlobal(StringRef LocalName) const { - llvm::Optional ValueOr = get(LocalName); + std::optional ValueOr = get(LocalName); bool IsGlobal = false; if (!ValueOr) { IsGlobal = true; ValueOr = getLocalOrGlobal(LocalName); if (!ValueOr) - return None; + return std::nullopt; } T Result{}; if (!StringRef(*ValueOr).getAsInteger(10, Result)) return Result; diagnoseBadIntegerOption( IsGlobal ? Twine(LocalName) : NamePrefix + LocalName, *ValueOr); - return None; + return std::nullopt; } /// Read a named option from the ``Context`` and parse it as an @@ -266,20 +266,20 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback { /// /// Reads the option with the check-local name \p LocalName from the /// ``CheckOptions``. If the corresponding key is not present, return - /// ``None``. + /// ``std::nullopt``. /// /// If the corresponding key can't be parsed as a ``T``, emit a - /// diagnostic and return ``None``. + /// diagnostic and return ``std::nullopt``. /// /// \ref clang::tidy::OptionEnumMapping must be specialized for ``T`` to /// supply the mapping required to convert between ``T`` and a string. template - std::enable_if_t::value, llvm::Optional> + std::enable_if_t::value, std::optional> get(StringRef LocalName, bool IgnoreCase = false) const { - if (llvm::Optional ValueOr = + if (std::optional ValueOr = getEnumInt(LocalName, typeEraseMapping(), false, IgnoreCase)) return static_cast(*ValueOr); - return None; + return std::nullopt; } /// Read a named option from the ``Context`` and parse it as an @@ -306,20 +306,20 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback { /// Reads the option with the check-local name \p LocalName from local or /// global ``CheckOptions``. Gets local option first. If local is not /// present, falls back to get global option. If global option is not - /// present either, returns ``None``. + /// present either, returns ``std::nullopt``. /// /// If the corresponding key can't be parsed as a ``T``, emit a - /// diagnostic and return ``None``. + /// diagnostic and return ``std::nullopt``. /// /// \ref clang::tidy::OptionEnumMapping must be specialized for ``T`` to /// supply the mapping required to convert between ``T`` and a string. template - std::enable_if_t::value, llvm::Optional> + std::enable_if_t::value, std::optional> getLocalOrGlobal(StringRef LocalName, bool IgnoreCase = false) const { - if (llvm::Optional ValueOr = + if (std::optional ValueOr = getEnumInt(LocalName, typeEraseMapping(), true, IgnoreCase)) return static_cast(*ValueOr); - return None; + return std::nullopt; } /// Read a named option from the ``Context`` and parse it as an @@ -378,9 +378,9 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback { private: using NameAndValue = std::pair; - llvm::Optional getEnumInt(StringRef LocalName, - ArrayRef Mapping, - bool CheckGlobal, bool IgnoreCase) const; + std::optional getEnumInt(StringRef LocalName, + ArrayRef Mapping, + bool CheckGlobal, bool IgnoreCase) const; template std::enable_if_t::value, std::vector> @@ -407,7 +407,6 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback { private: void run(const ast_matchers::MatchFinder::MatchResult &Result) override; - StringRef getID() const override { return CheckName; } std::string CheckName; ClangTidyContext *Context; @@ -422,18 +421,19 @@ class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback { bool areDiagsSelfContained() const { return Context->areDiagsSelfContained(); } + StringRef getID() const override { return CheckName; } }; /// Read a named option from the ``Context`` and parse it as a bool. /// /// Reads the option with the check-local name \p LocalName from the /// ``CheckOptions``. If the corresponding key is not present, return -/// ``None``. +/// ``std::nullopt``. /// /// If the corresponding key can't be parsed as a bool, emit a -/// diagnostic and return ``None``. +/// diagnostic and return ``std::nullopt``. template <> -llvm::Optional +std::optional ClangTidyCheck::OptionsView::get(StringRef LocalName) const; /// Read a named option from the ``Context`` and parse it as a bool. @@ -445,7 +445,7 @@ ClangTidyCheck::OptionsView::get(StringRef LocalName) const; /// If the corresponding key can't be parsed as a bool, emit a /// diagnostic and return \p Default. template <> -llvm::Optional +std::optional ClangTidyCheck::OptionsView::getLocalOrGlobal(StringRef LocalName) const; /// Stores an option with the check-local name \p LocalName with diff --git a/ClangTidyDiagnosticConsumer.h b/ClangTidyDiagnosticConsumer.h index 261e4f7..15f1b67 100644 --- a/ClangTidyDiagnosticConsumer.h +++ b/ClangTidyDiagnosticConsumer.h @@ -11,12 +11,14 @@ #include "ClangTidyOptions.h" #include "ClangTidyProfiling.h" +#include "FileExtensionsSet.h" #include "NoLintDirectiveHandler.h" #include "clang/Basic/Diagnostic.h" #include "clang/Tooling/Core/Diagnostic.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/StringSet.h" #include "llvm/Support/Regex.h" +#include namespace clang { @@ -159,6 +161,14 @@ class ClangTidyContext { /// \c CurrentFile. ClangTidyOptions getOptionsForFile(StringRef File) const; + const FileExtensionsSet &getHeaderFileExtensions() const { + return HeaderFileExtensions; + } + + const FileExtensionsSet &getImplementationFileExtensions() const { + return ImplementationFileExtensions; + } + /// Returns \c ClangTidyStats containing issued and ignored diagnostic /// counters. const ClangTidyStats &getStats() const { return Stats; } @@ -169,7 +179,7 @@ class ClangTidyContext { /// Control storage of profile date. void setProfileStoragePrefix(StringRef ProfilePrefix); - llvm::Optional + std::optional getProfileStorageParams() const; /// Should be called when starting to process new translation unit. @@ -220,6 +230,9 @@ class ClangTidyContext { std::unique_ptr CheckFilter; std::unique_ptr WarningAsErrorFilter; + FileExtensionsSet HeaderFileExtensions; + FileExtensionsSet ImplementationFileExtensions; + LangOptions LangOpts; ClangTidyStats Stats; diff --git a/ClangTidyForceLinker.h b/ClangTidyForceLinker.h index 2691d90..adde913 100644 --- a/ClangTidyForceLinker.h +++ b/ClangTidyForceLinker.h @@ -12,8 +12,7 @@ #include "clang-tidy-config.h" #include "llvm/Support/Compiler.h" -namespace clang { -namespace tidy { +namespace clang::tidy { // This anchor is used to force the linker to link the AbseilModule. extern volatile int AbseilModuleAnchorSource; @@ -138,7 +137,6 @@ extern volatile int ZirconModuleAnchorSource; static int LLVM_ATTRIBUTE_UNUSED ZirconModuleAnchorDestination = ZirconModuleAnchorSource; -} // namespace tidy -} // namespace clang +} // namespace clang::tidy #endif diff --git a/ClangTidyModule.h b/ClangTidyModule.h index f44457d..0e55c1e 100644 --- a/ClangTidyModule.h +++ b/ClangTidyModule.h @@ -15,8 +15,7 @@ #include #include -namespace clang { -namespace tidy { +namespace clang::tidy { class ClangTidyCheck; class ClangTidyContext; @@ -65,11 +64,11 @@ class ClangTidyCheckFactories { /// Create instances of checks that are enabled. std::vector> - createChecks(ClangTidyContext *Context); + createChecks(ClangTidyContext *Context) const; /// Create instances of checks that are enabled for the current Language. std::vector> - createChecksForLanguage(ClangTidyContext *Context); + createChecksForLanguage(ClangTidyContext *Context) const; typedef llvm::StringMap FactoryMap; FactoryMap::const_iterator begin() const { return Factories.begin(); } @@ -94,7 +93,6 @@ class ClangTidyModule { virtual ClangTidyOptions getModuleOptions(); }; -} // end namespace tidy -} // end namespace clang +} // namespace clang::tidy #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H diff --git a/ClangTidyModuleRegistry.h b/ClangTidyModuleRegistry.h index 891671a..30ffe18 100644 --- a/ClangTidyModuleRegistry.h +++ b/ClangTidyModuleRegistry.h @@ -12,12 +12,10 @@ #include "ClangTidyModule.h" #include "llvm/Support/Registry.h" -namespace clang { -namespace tidy { +namespace clang::tidy { typedef llvm::Registry ClangTidyModuleRegistry; -} // end namespace tidy -} // end namespace clang +} // namespace clang::tidy #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULEREGISTRY_H diff --git a/ClangTidyOptions.h b/ClangTidyOptions.h index 3f09d39..b3b7143 100644 --- a/ClangTidyOptions.h +++ b/ClangTidyOptions.h @@ -10,20 +10,19 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H #include "llvm/ADT/IntrusiveRefCntPtr.h" -#include "llvm/ADT/Optional.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/ErrorOr.h" #include "llvm/Support/MemoryBufferRef.h" #include "llvm/Support/VirtualFileSystem.h" #include +#include #include #include #include #include -namespace clang { -namespace tidy { +namespace clang::tidy { /// Contains a list of line ranges in a single file. struct FileFilter { @@ -63,21 +62,29 @@ struct ClangTidyOptions { /// Creates a new \c ClangTidyOptions instance combined from all fields /// of this instance overridden by the fields of \p Other that have a value. /// \p Order specifies precedence of \p Other option. - LLVM_NODISCARD ClangTidyOptions merge(const ClangTidyOptions &Other, - unsigned Order) const; + [[nodiscard]] ClangTidyOptions merge(const ClangTidyOptions &Other, + unsigned Order) const; /// Checks filter. - llvm::Optional Checks; + std::optional Checks; /// WarningsAsErrors filter. - llvm::Optional WarningsAsErrors; + std::optional WarningsAsErrors; + + /// File extensions to consider to determine if a given diagnostic is located + /// in a header file. + std::optional> HeaderFileExtensions; + + /// File extensions to consider to determine if a given diagnostic is located + /// is located in an implementation file. + std::optional> ImplementationFileExtensions; /// Output warnings from headers matching this filter. Warnings from /// main files will always be displayed. - llvm::Optional HeaderFilterRegex; + std::optional HeaderFilterRegex; /// Output warnings from system headers matching \c HeaderFilterRegex. - llvm::Optional SystemHeaders; + std::optional SystemHeaders; /// Format code around applied fixes with clang-format using this /// style. @@ -91,25 +98,25 @@ struct ClangTidyOptions { /// * '{inline-formatting-style-in-yaml-format}'. /// /// See clang-format documentation for more about configuring format style. - llvm::Optional FormatStyle; + std::optional FormatStyle; /// Specifies the name or e-mail of the user running clang-tidy. /// /// This option is used, for example, to place the correct user name in TODO() /// comments in the relevant check. - llvm::Optional User; + std::optional User; /// Helper structure for storing option value with priority of the value. struct ClangTidyValue { - ClangTidyValue() : Value(), Priority(0) {} - ClangTidyValue(const char *Value) : Value(Value), Priority(0) {} + ClangTidyValue() = default; + ClangTidyValue(const char *Value) : Value(Value) {} ClangTidyValue(llvm::StringRef Value, unsigned Priority = 0) : Value(Value), Priority(Priority) {} std::string Value; /// Priority stores relative precedence of the value loaded from config /// files to disambiguate local vs global value from different levels. - unsigned Priority; + unsigned Priority = 0; }; typedef std::pair StringPair; typedef llvm::StringMap OptionMap; @@ -120,10 +127,10 @@ struct ClangTidyOptions { typedef std::vector ArgList; /// Add extra compilation arguments to the end of the list. - llvm::Optional ExtraArgs; + std::optional ExtraArgs; /// Add extra compilation arguments to the start of the list. - llvm::Optional ExtraArgsBefore; + std::optional ExtraArgsBefore; /// Only used in the FileOptionsProvider and ConfigOptionsProvider. If true /// and using a FileOptionsProvider, it will take a configuration file in the @@ -132,10 +139,10 @@ struct ClangTidyOptions { /// config on top of any configuration file it finds in the directory using /// the same logic as FileOptionsProvider. If false or missing, only this /// configuration file will be used. - llvm::Optional InheritParentConfig; + std::optional InheritParentConfig; /// Use colors in diagnostics. If missing, it will be auto detected. - llvm::Optional UseColor; + std::optional UseColor; }; /// Abstract interface for retrieving various ClangTidy options. @@ -231,7 +238,7 @@ class FileOptionsBaseProvider : public DefaultOptionsProvider { /// Try to read configuration files from \p Directory using registered /// \c ConfigHandlers. - llvm::Optional tryReadConfigFile(llvm::StringRef Directory); + std::optional tryReadConfigFile(llvm::StringRef Directory); llvm::StringMap CachedOptions; ClangTidyOptions OverrideOptions; @@ -320,7 +327,6 @@ parseConfigurationWithDiags(llvm::MemoryBufferRef Config, DiagCallback Handler); /// Serializes configuration to a YAML-encoded string. std::string configurationAsText(const ClangTidyOptions &Options); -} // end namespace tidy -} // end namespace clang +} // namespace clang::tidy #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H diff --git a/ClangTidyProfiling.h b/ClangTidyProfiling.h index 9487a34..b6f7d66 100644 --- a/ClangTidyProfiling.h +++ b/ClangTidyProfiling.h @@ -9,18 +9,17 @@ #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYPROFILING_H #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYPROFILING_H -#include "llvm/ADT/Optional.h" #include "llvm/ADT/StringMap.h" #include "llvm/Support/Chrono.h" #include "llvm/Support/Timer.h" +#include #include namespace llvm { class raw_ostream; } // namespace llvm -namespace clang { -namespace tidy { +namespace clang::tidy { class ClangTidyProfiling { public: @@ -35,9 +34,9 @@ class ClangTidyProfiling { }; private: - llvm::Optional TG; + std::optional TG; - llvm::Optional Storage; + std::optional Storage; void printUserFriendlyTable(llvm::raw_ostream &OS); void printAsJSON(llvm::raw_ostream &OS); @@ -49,12 +48,11 @@ class ClangTidyProfiling { ClangTidyProfiling() = default; - ClangTidyProfiling(llvm::Optional Storage); + ClangTidyProfiling(std::optional Storage); ~ClangTidyProfiling(); }; -} // end namespace tidy -} // end namespace clang +} // namespace clang::tidy #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYPROFILING_H diff --git a/FileExtensionsSet.h b/FileExtensionsSet.h new file mode 100644 index 0000000..417b1b6 --- /dev/null +++ b/FileExtensionsSet.h @@ -0,0 +1,19 @@ +//===--- FileExtensionsSet.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_FILE_EXTENSIONS_SET_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FILE_EXTENSIONS_SET_H + +#include "llvm/ADT/SmallSet.h" +#include "llvm/ADT/StringRef.h" + +namespace clang::tidy { +typedef llvm::SmallSet FileExtensionsSet; +} // namespace clang::tidy + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FILE_EXTENSIONS_SET_H diff --git a/GlobList.h b/GlobList.h index 3eec92e..44af182 100644 --- a/GlobList.h +++ b/GlobList.h @@ -15,8 +15,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/Support/Regex.h" -namespace clang { -namespace tidy { +namespace clang::tidy { /// Read-only set of strings represented as a list of positive and negative /// globs. @@ -62,7 +61,6 @@ class CachedGlobList final : public GlobList { mutable llvm::StringMap Cache; }; -} // namespace tidy -} // namespace clang +} // namespace clang::tidy #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_GLOBLIST_H diff --git a/NoLintDirectiveHandler.h b/NoLintDirectiveHandler.h index db9fd59..e862195 100644 --- a/NoLintDirectiveHandler.h +++ b/NoLintDirectiveHandler.h @@ -13,18 +13,15 @@ #include "llvm/ADT/StringRef.h" #include -namespace clang { -namespace tooling { +namespace clang::tooling { struct Diagnostic; -} // namespace tooling -} // namespace clang +} // namespace clang::tooling namespace llvm { template class SmallVectorImpl; } // namespace llvm -namespace clang { -namespace tidy { +namespace clang::tidy { /// This class is used to locate NOLINT comments in the file being analyzed, to /// decide whether a diagnostic should be suppressed. @@ -45,7 +42,6 @@ class NoLintDirectiveHandler { std::unique_ptr PImpl; }; -} // namespace tidy -} // namespace clang +} // namespace clang::tidy #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_NOLINTDIRECTIVEHANDLER_H diff --git a/tool/CMakeLists.txt b/tool/CMakeLists.txt index 4d00fa4..5d9de7e 100644 --- a/tool/CMakeLists.txt +++ b/tool/CMakeLists.txt @@ -57,7 +57,9 @@ install(PROGRAMS run_O2CodeChecker.py DESTINATION bin) # we need to install the builtin headers in a path which is searched by the tool # FIXME: a soft link would be better -install(DIRECTORY ${LLVM_LIBRARY_DIR}/clang/${LLVM_PACKAGE_VERSION}/include DESTINATION lib/clang/${LLVM_PACKAGE_VERSION}) +string(REPLACE "." ";" LLVM_PACKAGE_VERSION_LIST ${LLVM_PACKAGE_VERSION}) +list(GET LLVM_PACKAGE_VERSION_LIST 0 LLVM_PACKAGE_VERSION_MAJOR) +install(DIRECTORY ${LLVM_LIBRARY_DIR}/clang/${LLVM_PACKAGE_VERSION_MAJOR}/include DESTINATION lib/clang/${LLVM_PACKAGE_VERSION_MAJOR}) IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") # On MacOS we need to do the same with C++ headers since they are in a non-standard location