@@ -67,12 +67,15 @@ template <> struct MappingTraits<ClangTidyOptions::StringPair> {
67
67
68
68
struct NOptionMap {
69
69
NOptionMap (IO &) {}
70
- NOptionMap (IO &, const ClangTidyOptions::OptionMap &OptionMap)
71
- : Options(OptionMap.begin(), OptionMap.end()) {}
70
+ NOptionMap (IO &, const ClangTidyOptions::OptionMap &OptionMap) {
71
+ Options.reserve (OptionMap.size ());
72
+ for (const auto &KeyValue : OptionMap)
73
+ Options.emplace_back (KeyValue.first , KeyValue.second .Value );
74
+ }
72
75
ClangTidyOptions::OptionMap denormalize (IO &) {
73
76
ClangTidyOptions::OptionMap Map;
74
77
for (const auto &KeyValue : Options)
75
- Map[KeyValue.first ] = KeyValue.second ;
78
+ Map[KeyValue.first ] = ClangTidyOptions::ClangTidyValue ( KeyValue.second ) ;
76
79
return Map;
77
80
}
78
81
std::vector<ClangTidyOptions::StringPair> Options;
@@ -92,6 +95,7 @@ template <> struct MappingTraits<ClangTidyOptions> {
92
95
IO.mapOptional (" CheckOptions" , NOpts->Options );
93
96
IO.mapOptional (" ExtraArgs" , Options.ExtraArgs );
94
97
IO.mapOptional (" ExtraArgsBefore" , Options.ExtraArgsBefore );
98
+ IO.mapOptional (" InheritParentConfig" , Options.InheritParentConfig );
95
99
}
96
100
};
97
101
@@ -109,10 +113,12 @@ ClangTidyOptions ClangTidyOptions::getDefaults() {
109
113
Options.SystemHeaders = false ;
110
114
Options.FormatStyle = " none" ;
111
115
Options.User = llvm::None;
116
+ unsigned Priority = 0 ;
112
117
for (ClangTidyModuleRegistry::iterator I = ClangTidyModuleRegistry::begin (),
113
118
E = ClangTidyModuleRegistry::end ();
114
119
I != E; ++I)
115
- Options = Options.mergeWith (I->instantiate ()->getModuleOptions ());
120
+ Options =
121
+ Options.mergeWith (I->instantiate ()->getModuleOptions (), ++Priority);
116
122
return Options;
117
123
}
118
124
@@ -138,8 +144,8 @@ static void overrideValue(Optional<T> &Dest, const Optional<T> &Src) {
138
144
Dest = Src;
139
145
}
140
146
141
- ClangTidyOptions
142
- ClangTidyOptions::mergeWith ( const ClangTidyOptions &Other ) const {
147
+ ClangTidyOptions ClangTidyOptions::mergeWith ( const ClangTidyOptions &Other,
148
+ unsigned Priority ) const {
143
149
ClangTidyOptions Result = *this ;
144
150
145
151
mergeCommaSeparatedLists (Result.Checks , Other.Checks );
@@ -151,8 +157,10 @@ ClangTidyOptions::mergeWith(const ClangTidyOptions &Other) const {
151
157
mergeVectors (Result.ExtraArgs , Other.ExtraArgs );
152
158
mergeVectors (Result.ExtraArgsBefore , Other.ExtraArgsBefore );
153
159
154
- for (const auto &KeyValue : Other.CheckOptions )
155
- Result.CheckOptions [KeyValue.first ] = KeyValue.second ;
160
+ for (const auto &KeyValue : Other.CheckOptions ) {
161
+ Result.CheckOptions [KeyValue.first ] = ClangTidyValue (
162
+ KeyValue.second .Value , KeyValue.second .Priority + Priority);
163
+ }
156
164
157
165
return Result;
158
166
}
@@ -168,8 +176,9 @@ const char
168
176
ClangTidyOptions
169
177
ClangTidyOptionsProvider::getOptions (llvm::StringRef FileName) {
170
178
ClangTidyOptions Result;
179
+ unsigned Priority = 0 ;
171
180
for (const auto &Source : getRawOptions (FileName))
172
- Result = Result.mergeWith (Source.first );
181
+ Result = Result.mergeWith (Source.first , ++Priority );
173
182
return Result;
174
183
}
175
184
@@ -237,6 +246,7 @@ FileOptionsProvider::getRawOptions(StringRef FileName) {
237
246
DefaultOptionsProvider::getRawOptions (AbsoluteFilePath.str ());
238
247
OptionsSource CommandLineOptions (OverrideOptions,
239
248
OptionsSourceTypeCheckCommandLineOption);
249
+ size_t FirstFileConfig = RawOptions.size ();
240
250
// Look for a suitable configuration file in all parent directories of the
241
251
// file. Start with the immediate parent directory and move up.
242
252
StringRef Path = llvm::sys::path::parent_path (AbsoluteFilePath.str ());
@@ -256,15 +266,21 @@ FileOptionsProvider::getRawOptions(StringRef FileName) {
256
266
while (Path != CurrentPath) {
257
267
LLVM_DEBUG (llvm::dbgs ()
258
268
<< " Caching configuration for path " << Path << " .\n " );
259
- CachedOptions[Path] = *Result;
269
+ if (!CachedOptions.count (Path))
270
+ CachedOptions[Path] = *Result;
260
271
Path = llvm::sys::path::parent_path (Path);
261
272
}
262
273
CachedOptions[Path] = *Result;
263
274
264
275
RawOptions.push_back (*Result);
265
- break ;
276
+ if (!Result->first .InheritParentConfig ||
277
+ !*Result->first .InheritParentConfig )
278
+ break ;
266
279
}
267
280
}
281
+ // Reverse order of file configs because closer configs should have higher
282
+ // priority.
283
+ std::reverse (RawOptions.begin () + FirstFileConfig, RawOptions.end ());
268
284
RawOptions.push_back (CommandLineOptions);
269
285
return RawOptions;
270
286
}
0 commit comments