blob: 148593dabd5f0060027236b62771fd51957c4251 [file] [log] [blame]
Alexander Kornienkodad4acb2014-05-22 16:07:11 +00001//===--- 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 Kornienkod53d2682014-09-04 14:23:36 +000011#include "clang/Basic/LLVM.h"
12#include "llvm/ADT/SmallString.h"
Hans Wennborgfece4c62014-09-04 22:41:03 +000013#include "llvm/Support/Errc.h"
Alexander Kornienkod53d2682014-09-04 14:23:36 +000014#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 Kornienkodad4acb2014-05-22 16:07:11 +000018#include "llvm/Support/YAMLTraits.h"
Alexander Kornienkod53d2682014-09-04 14:23:36 +000019#include <utility>
20
21#define DEBUG_TYPE "clang-tidy-options"
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000022
Alexander Kornienkoa4695222014-06-05 13:31:45 +000023using clang::tidy::ClangTidyOptions;
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000024using clang::tidy::FileFilter;
25
26LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FileFilter)
27LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FileFilter::LineRange)
Benjamin Kramer39e40cc2014-09-23 14:46:55 +000028LLVM_YAML_IS_SEQUENCE_VECTOR(ClangTidyOptions::StringPair)
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000029
30namespace llvm {
31namespace yaml {
32
33// Map std::pair<int, int> to a JSON array of size 2.
34template <> 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
45template <> 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 Kornienko6e0cbc82014-09-12 08:53:36 +000061template <> 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
68struct 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 Kornienkoa4695222014-06-05 13:31:45 +000081template <> struct MappingTraits<ClangTidyOptions> {
82 static void mapping(IO &IO, ClangTidyOptions &Options) {
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +000083 MappingNormalization<NOptionMap, ClangTidyOptions::OptionMap> NOpts(
84 IO, Options.CheckOptions);
Alexander Kornienkoa4695222014-06-05 13:31:45 +000085 IO.mapOptional("Checks", Options.Checks);
86 IO.mapOptional("HeaderFilterRegex", Options.HeaderFilterRegex);
87 IO.mapOptional("AnalyzeTemporaryDtors", Options.AnalyzeTemporaryDtors);
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +000088 IO.mapOptional("CheckOptions", NOpts->Options);
Alexander Kornienkoa4695222014-06-05 13:31:45 +000089 }
90};
91
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000092} // namespace yaml
93} // namespace llvm
94
95namespace clang {
96namespace tidy {
97
Alexander Kornienkod53d2682014-09-04 14:23:36 +000098ClangTidyOptions
99ClangTidyOptions::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 Kornienko6e0cbc82014-09-12 08:53:36 +0000112
113 for (const auto &KeyValue : Other.CheckOptions)
114 Result.CheckOptions[KeyValue.first] = KeyValue.second;
115
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000116 return Result;
117}
118
119FileOptionsProvider::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
128static 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.
133const 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 Wennborgfece4c62014-09-04 22:41:03 +0000169 if (Result.getError() != llvm::errc::no_such_file_or_directory) {
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000170 llvm::errs() << "Error reading " << ConfigFileName << " from " << Path
171 << ": " << Result.getError().message() << "\n";
172 }
173 }
174}
175
176llvm::ErrorOr<ClangTidyOptions>
177FileOptionsProvider::TryReadConfigFile(StringRef Directory) {
178 assert(!Directory.empty());
179
180 ClangTidyOptions Options = DefaultOptionsProvider::getOptions(Directory);
181 if (!llvm::sys::fs::is_directory(Directory))
Hans Wennborgfece4c62014-09-04 22:41:03 +0000182 return make_error_code(llvm::errc::not_a_directory);
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000183
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 Wennborgfece4c62014-09-04 22:41:03 +0000194 return make_error_code(llvm::errc::no_such_file_or_directory);
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000195
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 Kornienko6e0cbc82014-09-12 08:53:36 +0000200 // 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 Kornienkod53d2682014-09-04 14:23:36 +0000204 if (std::error_code EC = parseConfiguration((*Text)->getBuffer(), Options))
205 return EC;
206 return Options.mergeWith(OverrideOptions);
207}
208
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000209/// \brief Parses -line-filter option and stores it to the \c Options.
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000210std::error_code parseLineFilter(StringRef LineFilter,
Rafael Espindola15c57842014-06-12 13:32:11 +0000211 clang::tidy::ClangTidyGlobalOptions &Options) {
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000212 llvm::yaml::Input Input(LineFilter);
213 Input >> Options.LineFilter;
214 return Input.error();
215}
216
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000217std::error_code parseConfiguration(StringRef Config,
Rafael Espindola15c57842014-06-12 13:32:11 +0000218 clang::tidy::ClangTidyOptions &Options) {
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000219 llvm::yaml::Input Input(Config);
220 Input >> Options;
221 return Input.error();
222}
223
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000224std::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 Kornienkodad4acb2014-05-22 16:07:11 +0000235} // namespace tidy
236} // namespace clang