Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 1 | //===--- ClangTidyOptions.cpp - clang-tidy ----------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "ClangTidyOptions.h" |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 11 | #include "clang/Basic/LLVM.h" |
| 12 | #include "llvm/ADT/SmallString.h" |
Hans Wennborg | fece4c6 | 2014-09-04 22:41:03 +0000 | [diff] [blame] | 13 | #include "llvm/Support/Errc.h" |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Debug.h" |
| 15 | #include "llvm/Support/FileSystem.h" |
| 16 | #include "llvm/Support/Path.h" |
| 17 | #include "llvm/Support/raw_ostream.h" |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 18 | #include "llvm/Support/YAMLTraits.h" |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 19 | #include <utility> |
| 20 | |
| 21 | #define DEBUG_TYPE "clang-tidy-options" |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 22 | |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 23 | using clang::tidy::ClangTidyOptions; |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 24 | using clang::tidy::FileFilter; |
| 25 | |
| 26 | LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FileFilter) |
| 27 | LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FileFilter::LineRange) |
Benjamin Kramer | 39e40cc | 2014-09-23 14:46:55 +0000 | [diff] [blame^] | 28 | LLVM_YAML_IS_SEQUENCE_VECTOR(ClangTidyOptions::StringPair) |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 29 | |
| 30 | namespace llvm { |
| 31 | namespace yaml { |
| 32 | |
| 33 | // Map std::pair<int, int> to a JSON array of size 2. |
| 34 | template <> struct SequenceTraits<FileFilter::LineRange> { |
| 35 | static size_t size(IO &IO, FileFilter::LineRange &Range) { |
| 36 | return Range.first == 0 ? 0 : Range.second == 0 ? 1 : 2; |
| 37 | } |
| 38 | static unsigned &element(IO &IO, FileFilter::LineRange &Range, size_t Index) { |
| 39 | if (Index > 1) |
| 40 | IO.setError("Too many elements in line range."); |
| 41 | return Index == 0 ? Range.first : Range.second; |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | template <> struct MappingTraits<FileFilter> { |
| 46 | static void mapping(IO &IO, FileFilter &File) { |
| 47 | IO.mapRequired("name", File.Name); |
| 48 | IO.mapOptional("lines", File.LineRanges); |
| 49 | } |
| 50 | static StringRef validate(IO &io, FileFilter &File) { |
| 51 | if (File.Name.empty()) |
| 52 | return "No file name specified"; |
| 53 | for (const FileFilter::LineRange &Range : File.LineRanges) { |
| 54 | if (Range.first <= 0 || Range.second <= 0) |
| 55 | return "Invalid line range"; |
| 56 | } |
| 57 | return StringRef(); |
| 58 | } |
| 59 | }; |
| 60 | |
Alexander Kornienko | 6e0cbc8 | 2014-09-12 08:53:36 +0000 | [diff] [blame] | 61 | template <> struct MappingTraits<ClangTidyOptions::StringPair> { |
| 62 | static void mapping(IO &IO, ClangTidyOptions::StringPair &KeyValue) { |
| 63 | IO.mapRequired("key", KeyValue.first); |
| 64 | IO.mapRequired("value", KeyValue.second); |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | struct NOptionMap { |
| 69 | NOptionMap(IO &) {} |
| 70 | NOptionMap(IO &, const ClangTidyOptions::OptionMap &OptionMap) |
| 71 | : Options(OptionMap.begin(), OptionMap.end()) {} |
| 72 | ClangTidyOptions::OptionMap denormalize(IO &) { |
| 73 | ClangTidyOptions::OptionMap Map; |
| 74 | for (const auto &KeyValue : Options) |
| 75 | Map[KeyValue.first] = KeyValue.second; |
| 76 | return Map; |
| 77 | } |
| 78 | std::vector<ClangTidyOptions::StringPair> Options; |
| 79 | }; |
| 80 | |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 81 | template <> struct MappingTraits<ClangTidyOptions> { |
| 82 | static void mapping(IO &IO, ClangTidyOptions &Options) { |
Alexander Kornienko | 6e0cbc8 | 2014-09-12 08:53:36 +0000 | [diff] [blame] | 83 | MappingNormalization<NOptionMap, ClangTidyOptions::OptionMap> NOpts( |
| 84 | IO, Options.CheckOptions); |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 85 | IO.mapOptional("Checks", Options.Checks); |
| 86 | IO.mapOptional("HeaderFilterRegex", Options.HeaderFilterRegex); |
| 87 | IO.mapOptional("AnalyzeTemporaryDtors", Options.AnalyzeTemporaryDtors); |
Alexander Kornienko | 6e0cbc8 | 2014-09-12 08:53:36 +0000 | [diff] [blame] | 88 | IO.mapOptional("CheckOptions", NOpts->Options); |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 89 | } |
| 90 | }; |
| 91 | |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 92 | } // namespace yaml |
| 93 | } // namespace llvm |
| 94 | |
| 95 | namespace clang { |
| 96 | namespace tidy { |
| 97 | |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 98 | ClangTidyOptions |
| 99 | ClangTidyOptions::mergeWith(const ClangTidyOptions &Other) const { |
| 100 | ClangTidyOptions Result = *this; |
| 101 | |
| 102 | // Merge comma-separated glob lists by appending the new value after a comma. |
| 103 | if (Other.Checks) |
| 104 | Result.Checks = |
| 105 | (Result.Checks && !Result.Checks->empty() ? *Result.Checks + "," : "") + |
| 106 | *Other.Checks; |
| 107 | |
| 108 | if (Other.HeaderFilterRegex) |
| 109 | Result.HeaderFilterRegex = Other.HeaderFilterRegex; |
| 110 | if (Other.AnalyzeTemporaryDtors) |
| 111 | Result.AnalyzeTemporaryDtors = Other.AnalyzeTemporaryDtors; |
Alexander Kornienko | 6e0cbc8 | 2014-09-12 08:53:36 +0000 | [diff] [blame] | 112 | |
| 113 | for (const auto &KeyValue : Other.CheckOptions) |
| 114 | Result.CheckOptions[KeyValue.first] = KeyValue.second; |
| 115 | |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 116 | return Result; |
| 117 | } |
| 118 | |
| 119 | FileOptionsProvider::FileOptionsProvider( |
| 120 | const ClangTidyGlobalOptions &GlobalOptions, |
| 121 | const ClangTidyOptions &FallbackOptions, |
| 122 | const ClangTidyOptions &OverrideOptions) |
| 123 | : DefaultOptionsProvider(GlobalOptions, FallbackOptions), |
| 124 | OverrideOptions(OverrideOptions) { |
| 125 | CachedOptions[""] = FallbackOptions.mergeWith(OverrideOptions); |
| 126 | } |
| 127 | |
| 128 | static const char ConfigFileName[] = ".clang-tidy"; |
| 129 | |
| 130 | // FIXME: This method has some common logic with clang::format::getStyle(). |
| 131 | // Consider pulling out common bits to a findParentFileWithName function or |
| 132 | // similar. |
| 133 | const ClangTidyOptions &FileOptionsProvider::getOptions(StringRef FileName) { |
| 134 | DEBUG(llvm::dbgs() << "Getting options for file " << FileName << "...\n"); |
| 135 | SmallString<256> FilePath(FileName); |
| 136 | |
| 137 | if (std::error_code EC = llvm::sys::fs::make_absolute(FilePath)) { |
| 138 | llvm::errs() << "Can't make absolute path from " << FileName << ": " |
| 139 | << EC.message() << "\n"; |
| 140 | // FIXME: Figure out what to do. |
| 141 | } else { |
| 142 | FileName = FilePath; |
| 143 | } |
| 144 | |
| 145 | // Look for a suitable configuration file in all parent directories of the |
| 146 | // file. Start with the immediate parent directory and move up. |
| 147 | StringRef Path = llvm::sys::path::parent_path(FileName); |
| 148 | for (StringRef CurrentPath = Path;; |
| 149 | CurrentPath = llvm::sys::path::parent_path(CurrentPath)) { |
| 150 | llvm::ErrorOr<ClangTidyOptions> Result = std::error_code(); |
| 151 | |
| 152 | auto Iter = CachedOptions.find(CurrentPath); |
| 153 | if (Iter != CachedOptions.end()) |
| 154 | Result = Iter->second; |
| 155 | |
| 156 | if (!Result) |
| 157 | Result = TryReadConfigFile(CurrentPath); |
| 158 | |
| 159 | if (Result) { |
| 160 | // Store cached value for all intermediate directories. |
| 161 | while (Path != CurrentPath) { |
| 162 | DEBUG(llvm::dbgs() << "Caching configuration for path " << Path |
| 163 | << ".\n"); |
| 164 | CachedOptions.GetOrCreateValue(Path, *Result); |
| 165 | Path = llvm::sys::path::parent_path(Path); |
| 166 | } |
| 167 | return CachedOptions.GetOrCreateValue(Path, *Result).getValue(); |
| 168 | } |
Hans Wennborg | fece4c6 | 2014-09-04 22:41:03 +0000 | [diff] [blame] | 169 | if (Result.getError() != llvm::errc::no_such_file_or_directory) { |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 170 | llvm::errs() << "Error reading " << ConfigFileName << " from " << Path |
| 171 | << ": " << Result.getError().message() << "\n"; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | llvm::ErrorOr<ClangTidyOptions> |
| 177 | FileOptionsProvider::TryReadConfigFile(StringRef Directory) { |
| 178 | assert(!Directory.empty()); |
| 179 | |
| 180 | ClangTidyOptions Options = DefaultOptionsProvider::getOptions(Directory); |
| 181 | if (!llvm::sys::fs::is_directory(Directory)) |
Hans Wennborg | fece4c6 | 2014-09-04 22:41:03 +0000 | [diff] [blame] | 182 | return make_error_code(llvm::errc::not_a_directory); |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 183 | |
| 184 | SmallString<128> ConfigFile(Directory); |
| 185 | llvm::sys::path::append(ConfigFile, ".clang-tidy"); |
| 186 | DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n"); |
| 187 | |
| 188 | bool IsFile = false; |
| 189 | // Ignore errors from is_regular_file: we only need to know if we can read |
| 190 | // the file or not. |
| 191 | llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile); |
| 192 | |
| 193 | if (!IsFile) |
Hans Wennborg | fece4c6 | 2014-09-04 22:41:03 +0000 | [diff] [blame] | 194 | return make_error_code(llvm::errc::no_such_file_or_directory); |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 195 | |
| 196 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text = |
| 197 | llvm::MemoryBuffer::getFile(ConfigFile.c_str()); |
| 198 | if (std::error_code EC = Text.getError()) |
| 199 | return EC; |
Alexander Kornienko | 6e0cbc8 | 2014-09-12 08:53:36 +0000 | [diff] [blame] | 200 | // Skip empty files, e.g. files opened for writing via shell output |
| 201 | // redirection. |
| 202 | if ((*Text)->getBuffer().empty()) |
| 203 | return make_error_code(llvm::errc::no_such_file_or_directory); |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 204 | if (std::error_code EC = parseConfiguration((*Text)->getBuffer(), Options)) |
| 205 | return EC; |
| 206 | return Options.mergeWith(OverrideOptions); |
| 207 | } |
| 208 | |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 209 | /// \brief Parses -line-filter option and stores it to the \c Options. |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 210 | std::error_code parseLineFilter(StringRef LineFilter, |
Rafael Espindola | 15c5784 | 2014-06-12 13:32:11 +0000 | [diff] [blame] | 211 | clang::tidy::ClangTidyGlobalOptions &Options) { |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 212 | llvm::yaml::Input Input(LineFilter); |
| 213 | Input >> Options.LineFilter; |
| 214 | return Input.error(); |
| 215 | } |
| 216 | |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 217 | std::error_code parseConfiguration(StringRef Config, |
Rafael Espindola | 15c5784 | 2014-06-12 13:32:11 +0000 | [diff] [blame] | 218 | clang::tidy::ClangTidyOptions &Options) { |
Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 219 | llvm::yaml::Input Input(Config); |
| 220 | Input >> Options; |
| 221 | return Input.error(); |
| 222 | } |
| 223 | |
Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame] | 224 | std::string configurationAsText(const ClangTidyOptions &Options) { |
| 225 | std::string Text; |
| 226 | llvm::raw_string_ostream Stream(Text); |
| 227 | llvm::yaml::Output Output(Stream); |
| 228 | // We use the same mapping method for input and output, so we need a non-const |
| 229 | // reference here. |
| 230 | ClangTidyOptions NonConstValue = Options; |
| 231 | Output << NonConstValue; |
| 232 | return Stream.str(); |
| 233 | } |
| 234 | |
Alexander Kornienko | dad4acb | 2014-05-22 16:07:11 +0000 | [diff] [blame] | 235 | } // namespace tidy |
| 236 | } // namespace clang |