2014-04-29 19:20:10 +04:00
|
|
|
//===--- ClangTidyOptions.h - clang-tidy ------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-09 19:52:33 +03:00
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H
|
2014-04-29 19:20:10 +04:00
|
|
|
|
2014-09-04 18:23:36 +04:00
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
2014-06-05 17:31:45 +04:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2014-09-04 18:23:36 +04:00
|
|
|
#include "llvm/Support/ErrorOr.h"
|
2014-10-20 16:29:15 +04:00
|
|
|
#include <functional>
|
2014-09-12 12:53:36 +04:00
|
|
|
#include <map>
|
2014-05-05 18:54:47 +04:00
|
|
|
#include <string>
|
2014-06-12 20:53:02 +04:00
|
|
|
#include <system_error>
|
2014-05-22 20:07:11 +04:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2014-05-05 18:54:47 +04:00
|
|
|
|
2014-04-29 19:20:10 +04:00
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
|
2014-06-05 17:31:45 +04:00
|
|
|
/// \brief Contains a list of line ranges in a single file.
|
2014-05-22 20:07:11 +04:00
|
|
|
struct FileFilter {
|
2014-06-05 17:31:45 +04:00
|
|
|
/// \brief File name.
|
2014-05-22 20:07:11 +04:00
|
|
|
std::string Name;
|
2014-06-05 17:31:45 +04:00
|
|
|
|
|
|
|
/// \brief LineRange is a pair<start, end> (inclusive).
|
2014-05-22 20:07:11 +04:00
|
|
|
typedef std::pair<unsigned, unsigned> LineRange;
|
2014-06-05 17:31:45 +04:00
|
|
|
|
|
|
|
/// \brief A list of line ranges in this file, for which we show warnings.
|
2014-05-22 20:07:11 +04:00
|
|
|
std::vector<LineRange> LineRanges;
|
|
|
|
};
|
|
|
|
|
2014-06-05 17:31:45 +04:00
|
|
|
/// \brief Global options. These options are neither stored nor read from
|
|
|
|
/// configuration files.
|
|
|
|
struct ClangTidyGlobalOptions {
|
2014-06-12 15:25:45 +04:00
|
|
|
/// \brief Output warnings from certain line ranges of certain files only.
|
|
|
|
/// If empty, no warnings will be filtered.
|
2014-06-05 17:31:45 +04:00
|
|
|
std::vector<FileFilter> LineFilter;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Contains options for clang-tidy. These options may be read from
|
|
|
|
/// configuration files, and may be different for different translation units.
|
2014-04-29 19:20:10 +04:00
|
|
|
struct ClangTidyOptions {
|
2014-09-04 18:23:36 +04:00
|
|
|
/// \brief These options are used for all settings that haven't been
|
|
|
|
/// overridden by the \c OptionsProvider.
|
|
|
|
///
|
2014-10-16 15:27:57 +04:00
|
|
|
/// Allow no checks and no headers by default. This method initializes
|
|
|
|
/// check-specific options by calling \c ClangTidyModule::getModuleOptions()
|
|
|
|
/// of each registered \c ClangTidyModule.
|
|
|
|
static ClangTidyOptions getDefaults();
|
2014-09-04 18:23:36 +04:00
|
|
|
|
|
|
|
/// \brief Creates a new \c ClangTidyOptions instance combined from all fields
|
|
|
|
/// of this instance overridden by the fields of \p Other that have a value.
|
|
|
|
ClangTidyOptions mergeWith(const ClangTidyOptions &Other) const;
|
2014-06-05 17:31:45 +04:00
|
|
|
|
|
|
|
/// \brief Checks filter.
|
2014-09-04 18:23:36 +04:00
|
|
|
llvm::Optional<std::string> Checks;
|
2014-05-22 20:07:11 +04:00
|
|
|
|
2016-01-13 20:36:41 +03:00
|
|
|
/// \brief WarningsAsErrors filter.
|
|
|
|
llvm::Optional<std::string> WarningsAsErrors;
|
|
|
|
|
2014-06-05 17:31:45 +04:00
|
|
|
/// \brief Output warnings from headers matching this filter. Warnings from
|
|
|
|
/// main files will always be displayed.
|
2014-09-04 18:23:36 +04:00
|
|
|
llvm::Optional<std::string> HeaderFilterRegex;
|
2014-05-22 20:07:11 +04:00
|
|
|
|
2014-10-29 01:16:13 +03:00
|
|
|
/// \brief Output warnings from system headers matching \c HeaderFilterRegex.
|
|
|
|
llvm::Optional<bool> SystemHeaders;
|
|
|
|
|
2014-06-05 17:31:45 +04:00
|
|
|
/// \brief Turns on temporary destructor-based analysis.
|
2014-09-04 18:23:36 +04:00
|
|
|
llvm::Optional<bool> AnalyzeTemporaryDtors;
|
2014-09-12 12:53:36 +04:00
|
|
|
|
2014-09-24 22:36:03 +04:00
|
|
|
/// \brief 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<std::string> User;
|
|
|
|
|
2014-09-12 12:53:36 +04:00
|
|
|
typedef std::pair<std::string, std::string> StringPair;
|
|
|
|
typedef std::map<std::string, std::string> OptionMap;
|
|
|
|
|
|
|
|
/// \brief Key-value mapping used to store check-specific options.
|
|
|
|
OptionMap CheckOptions;
|
2015-11-09 19:28:11 +03:00
|
|
|
|
|
|
|
typedef std::vector<std::string> ArgList;
|
|
|
|
|
|
|
|
/// \brief Add extra compilation arguments to the end of the list.
|
|
|
|
llvm::Optional<ArgList> ExtraArgs;
|
|
|
|
|
|
|
|
/// \brief Add extra compilation arguments to the start of the list.
|
|
|
|
llvm::Optional<ArgList> ExtraArgsBefore;
|
2014-04-29 19:20:10 +04:00
|
|
|
};
|
|
|
|
|
2014-06-05 17:31:45 +04:00
|
|
|
/// \brief Abstract interface for retrieving various ClangTidy options.
|
|
|
|
class ClangTidyOptionsProvider {
|
|
|
|
public:
|
2015-10-21 00:45:52 +03:00
|
|
|
virtual ~ClangTidyOptionsProvider() {}
|
2014-06-05 17:31:45 +04:00
|
|
|
|
|
|
|
/// \brief Returns global options, which are independent of the file.
|
|
|
|
virtual const ClangTidyGlobalOptions &getGlobalOptions() = 0;
|
|
|
|
|
|
|
|
/// \brief Returns options applying to a specific translation unit with the
|
|
|
|
/// specified \p FileName.
|
2015-01-20 12:48:51 +03:00
|
|
|
virtual ClangTidyOptions getOptions(llvm::StringRef FileName) = 0;
|
2014-06-05 17:31:45 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Implementation of the \c ClangTidyOptionsProvider interface, which
|
|
|
|
/// returns the same options for all files.
|
|
|
|
class DefaultOptionsProvider : public ClangTidyOptionsProvider {
|
|
|
|
public:
|
|
|
|
DefaultOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
|
|
|
|
const ClangTidyOptions &Options)
|
|
|
|
: GlobalOptions(GlobalOptions), DefaultOptions(Options) {}
|
|
|
|
const ClangTidyGlobalOptions &getGlobalOptions() override {
|
|
|
|
return GlobalOptions;
|
|
|
|
}
|
2015-01-20 12:48:51 +03:00
|
|
|
ClangTidyOptions getOptions(llvm::StringRef /*FileName*/) override {
|
2014-06-05 17:31:45 +04:00
|
|
|
return DefaultOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ClangTidyGlobalOptions GlobalOptions;
|
|
|
|
ClangTidyOptions DefaultOptions;
|
|
|
|
};
|
|
|
|
|
2014-09-04 18:23:36 +04:00
|
|
|
/// \brief Implementation of the \c ClangTidyOptionsProvider interface, which
|
2014-10-20 16:29:15 +04:00
|
|
|
/// tries to find a configuration file in the closest parent directory of each
|
|
|
|
/// source file.
|
|
|
|
///
|
|
|
|
/// By default, files named ".clang-tidy" will be considered, and the
|
|
|
|
/// \c clang::tidy::parseConfiguration function will be used for parsing, but a
|
|
|
|
/// custom set of configuration file names and parsing functions can be
|
|
|
|
/// specified using the appropriate constructor.
|
2014-09-04 18:23:36 +04:00
|
|
|
class FileOptionsProvider : public DefaultOptionsProvider {
|
|
|
|
public:
|
2014-10-20 16:29:15 +04:00
|
|
|
// \brief A pair of configuration file base name and a function parsing
|
|
|
|
// configuration from text in the corresponding format.
|
|
|
|
typedef std::pair<std::string, std::function<llvm::ErrorOr<ClangTidyOptions>(
|
|
|
|
llvm::StringRef)>> ConfigFileHandler;
|
|
|
|
|
|
|
|
/// \brief Configuration file handlers listed in the order of priority.
|
|
|
|
///
|
|
|
|
/// Custom configuration file formats can be supported by constructing the
|
|
|
|
/// list of handlers and passing it to the appropriate \c FileOptionsProvider
|
|
|
|
/// constructor. E.g. initialization of a \c FileOptionsProvider with support
|
|
|
|
/// of a custom configuration file format for files named ".my-tidy-config"
|
|
|
|
/// could look similar to this:
|
|
|
|
/// \code
|
|
|
|
/// FileOptionsProvider::ConfigFileHandlers ConfigHandlers;
|
|
|
|
/// ConfigHandlers.emplace_back(".my-tidy-config", parseMyConfigFormat);
|
|
|
|
/// ConfigHandlers.emplace_back(".clang-tidy", parseConfiguration);
|
|
|
|
/// return llvm::make_unique<FileOptionsProvider>(
|
|
|
|
/// GlobalOptions, DefaultOptions, OverrideOptions, ConfigHandlers);
|
|
|
|
/// \endcode
|
|
|
|
///
|
|
|
|
/// With the order of handlers shown above, the ".my-tidy-config" file would
|
|
|
|
/// take precedence over ".clang-tidy" if both reside in the same directory.
|
|
|
|
typedef std::vector<ConfigFileHandler> ConfigFileHandlers;
|
|
|
|
|
2014-09-04 18:23:36 +04:00
|
|
|
/// \brief Initializes the \c FileOptionsProvider instance.
|
|
|
|
///
|
|
|
|
/// \param GlobalOptions are just stored and returned to the caller of
|
|
|
|
/// \c getGlobalOptions.
|
|
|
|
///
|
2014-09-24 22:36:03 +04:00
|
|
|
/// \param DefaultOptions are used for all settings not specified in a
|
2014-10-20 16:29:15 +04:00
|
|
|
/// configuration file.
|
2014-09-04 18:23:36 +04:00
|
|
|
///
|
|
|
|
/// If any of the \param OverrideOptions fields are set, they will override
|
|
|
|
/// whatever options are read from the configuration file.
|
|
|
|
FileOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
|
2014-09-24 22:36:03 +04:00
|
|
|
const ClangTidyOptions &DefaultOptions,
|
2014-09-04 18:23:36 +04:00
|
|
|
const ClangTidyOptions &OverrideOptions);
|
2014-10-20 16:29:15 +04:00
|
|
|
|
|
|
|
/// \brief Initializes the \c FileOptionsProvider instance with a custom set
|
|
|
|
/// of configuration file handlers.
|
|
|
|
///
|
|
|
|
/// \param GlobalOptions are just stored and returned to the caller of
|
|
|
|
/// \c getGlobalOptions.
|
|
|
|
///
|
|
|
|
/// \param DefaultOptions are used for all settings not specified in a
|
|
|
|
/// configuration file.
|
|
|
|
///
|
|
|
|
/// If any of the \param OverrideOptions fields are set, they will override
|
|
|
|
/// whatever options are read from the configuration file.
|
|
|
|
///
|
|
|
|
/// \param ConfigHandlers specifies a custom set of configuration file
|
|
|
|
/// handlers. Each handler is a pair of configuration file name and a function
|
|
|
|
/// that can parse configuration from this file type. The configuration files
|
|
|
|
/// in each directory are searched for in the order of appearance in
|
|
|
|
/// \p ConfigHandlers.
|
|
|
|
FileOptionsProvider(const ClangTidyGlobalOptions &GlobalOptions,
|
|
|
|
const ClangTidyOptions &DefaultOptions,
|
|
|
|
const ClangTidyOptions &OverrideOptions,
|
|
|
|
const ConfigFileHandlers &ConfigHandlers);
|
|
|
|
|
2015-01-20 12:48:51 +03:00
|
|
|
ClangTidyOptions getOptions(llvm::StringRef FileName) override;
|
2014-09-04 18:23:36 +04:00
|
|
|
|
2015-08-12 22:29:57 +03:00
|
|
|
protected:
|
2014-10-20 16:29:15 +04:00
|
|
|
/// \brief Try to read configuration files from \p Directory using registered
|
|
|
|
/// \c ConfigHandlers.
|
|
|
|
llvm::Optional<ClangTidyOptions> TryReadConfigFile(llvm::StringRef Directory);
|
2014-09-04 18:23:36 +04:00
|
|
|
|
|
|
|
llvm::StringMap<ClangTidyOptions> CachedOptions;
|
|
|
|
ClangTidyOptions OverrideOptions;
|
2014-10-20 16:29:15 +04:00
|
|
|
ConfigFileHandlers ConfigHandlers;
|
2014-09-04 18:23:36 +04:00
|
|
|
};
|
|
|
|
|
2014-06-05 17:31:45 +04:00
|
|
|
/// \brief Parses LineFilter from JSON and stores it to the \p Options.
|
2014-09-04 18:23:36 +04:00
|
|
|
std::error_code parseLineFilter(llvm::StringRef LineFilter,
|
|
|
|
ClangTidyGlobalOptions &Options);
|
2014-06-05 17:31:45 +04:00
|
|
|
|
2014-09-24 22:36:03 +04:00
|
|
|
/// \brief Parses configuration from JSON and returns \c ClangTidyOptions or an
|
|
|
|
/// error.
|
|
|
|
llvm::ErrorOr<ClangTidyOptions> parseConfiguration(llvm::StringRef Config);
|
2014-09-04 18:23:36 +04:00
|
|
|
|
|
|
|
/// \brief Serializes configuration to a YAML-encoded string.
|
|
|
|
std::string configurationAsText(const ClangTidyOptions &Options);
|
2014-05-22 20:07:11 +04:00
|
|
|
|
2014-04-29 19:20:10 +04:00
|
|
|
} // end namespace tidy
|
|
|
|
} // end namespace clang
|
|
|
|
|
2015-03-09 19:52:33 +03:00
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYOPTIONS_H
|