blob: 6bbb9bc4465d0cef438a43878ba363889d4e3bea [file] [log] [blame]
Alexander Kornienkod0488d42017-05-09 14:56:28 +00001//===--- tools/extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp ----------=== //
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/// \file This file implements ClangTidyDiagnosticConsumer, ClangTidyContext
11/// and ClangTidyError classes.
12///
13/// This tool uses the Clang Tooling infrastructure, see
14/// http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
15/// for details on setting it up with LLVM source tree.
16///
17//===----------------------------------------------------------------------===//
18
19#include "ClangTidyDiagnosticConsumer.h"
20#include "ClangTidyOptions.h"
21#include "clang/AST/ASTDiagnostic.h"
22#include "clang/Basic/DiagnosticOptions.h"
23#include "clang/Frontend/DiagnosticRenderer.h"
24#include "llvm/ADT/SmallString.h"
25#include <tuple>
26#include <vector>
27using namespace clang;
28using namespace tidy;
29
30namespace {
31class ClangTidyDiagnosticRenderer : public DiagnosticRenderer {
32public:
33 ClangTidyDiagnosticRenderer(const LangOptions &LangOpts,
34 DiagnosticOptions *DiagOpts,
35 ClangTidyError &Error)
36 : DiagnosticRenderer(LangOpts, DiagOpts), Error(Error) {}
37
38protected:
39 void emitDiagnosticMessage(SourceLocation Loc, PresumedLoc PLoc,
40 DiagnosticsEngine::Level Level, StringRef Message,
41 ArrayRef<CharSourceRange> Ranges,
42 const SourceManager *SM,
43 DiagOrStoredDiag Info) override {
44 // Remove check name from the message.
45 // FIXME: Remove this once there's a better way to pass check names than
46 // appending the check name to the message in ClangTidyContext::diag and
47 // using getCustomDiagID.
48 std::string CheckNameInMessage = " [" + Error.DiagnosticName + "]";
49 if (Message.endswith(CheckNameInMessage))
50 Message = Message.substr(0, Message.size() - CheckNameInMessage.size());
51
52 auto TidyMessage = Loc.isValid()
53 ? tooling::DiagnosticMessage(Message, *SM, Loc)
54 : tooling::DiagnosticMessage(Message);
55 if (Level == DiagnosticsEngine::Note) {
56 Error.Notes.push_back(TidyMessage);
57 return;
58 }
59 assert(Error.Message.Message.empty() && "Overwriting a diagnostic message");
60 Error.Message = TidyMessage;
61 }
62
63 void emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc,
64 DiagnosticsEngine::Level Level,
65 ArrayRef<CharSourceRange> Ranges,
66 const SourceManager &SM) override {}
67
68 void emitCodeContext(SourceLocation Loc, DiagnosticsEngine::Level Level,
69 SmallVectorImpl<CharSourceRange> &Ranges,
70 ArrayRef<FixItHint> Hints,
71 const SourceManager &SM) override {
72 assert(Loc.isValid());
73 for (const auto &FixIt : Hints) {
74 CharSourceRange Range = FixIt.RemoveRange;
75 assert(Range.getBegin().isValid() && Range.getEnd().isValid() &&
76 "Invalid range in the fix-it hint.");
77 assert(Range.getBegin().isFileID() && Range.getEnd().isFileID() &&
78 "Only file locations supported in fix-it hints.");
79
80 tooling::Replacement Replacement(SM, Range, FixIt.CodeToInsert);
81 llvm::Error Err = Error.Fix[Replacement.getFilePath()].add(Replacement);
82 // FIXME: better error handling (at least, don't let other replacements be
83 // applied).
84 if (Err) {
85 llvm::errs() << "Fix conflicts with existing fix! "
86 << llvm::toString(std::move(Err)) << "\n";
87 assert(false && "Fix conflicts with existing fix!");
88 }
89 }
90 }
91
92 void emitIncludeLocation(SourceLocation Loc, PresumedLoc PLoc,
93 const SourceManager &SM) override {}
94
95 void emitImportLocation(SourceLocation Loc, PresumedLoc PLoc,
96 StringRef ModuleName,
97 const SourceManager &SM) override {}
98
99 void emitBuildingModuleLocation(SourceLocation Loc, PresumedLoc PLoc,
100 StringRef ModuleName,
101 const SourceManager &SM) override {}
102
103 void endDiagnostic(DiagOrStoredDiag D,
104 DiagnosticsEngine::Level Level) override {
105 assert(!Error.Message.Message.empty() && "Message has not been set");
106 }
107
108private:
109 ClangTidyError &Error;
110};
111} // end anonymous namespace
112
113ClangTidyError::ClangTidyError(StringRef CheckName,
114 ClangTidyError::Level DiagLevel,
115 StringRef BuildDirectory, bool IsWarningAsError)
116 : tooling::Diagnostic(CheckName, DiagLevel, BuildDirectory),
117 IsWarningAsError(IsWarningAsError) {}
118
119// Returns true if GlobList starts with the negative indicator ('-'), removes it
120// from the GlobList.
121static bool ConsumeNegativeIndicator(StringRef &GlobList) {
122 GlobList = GlobList.trim(' ');
123 if (GlobList.startswith("-")) {
124 GlobList = GlobList.substr(1);
125 return true;
126 }
127 return false;
128}
129// Converts first glob from the comma-separated list of globs to Regex and
130// removes it and the trailing comma from the GlobList.
131static llvm::Regex ConsumeGlob(StringRef &GlobList) {
132 StringRef UntrimmedGlob = GlobList.substr(0, GlobList.find(','));
133 StringRef Glob = UntrimmedGlob.trim(' ');
134 GlobList = GlobList.substr(UntrimmedGlob.size() + 1);
135 SmallString<128> RegexText("^");
136 StringRef MetaChars("()^$|*+?.[]\\{}");
137 for (char C : Glob) {
138 if (C == '*')
139 RegexText.push_back('.');
140 else if (MetaChars.find(C) != StringRef::npos)
141 RegexText.push_back('\\');
142 RegexText.push_back(C);
143 }
144 RegexText.push_back('$');
145 return llvm::Regex(RegexText);
146}
147
148GlobList::GlobList(StringRef Globs)
149 : Positive(!ConsumeNegativeIndicator(Globs)), Regex(ConsumeGlob(Globs)),
150 NextGlob(Globs.empty() ? nullptr : new GlobList(Globs)) {}
151
152bool GlobList::contains(StringRef S, bool Contains) {
153 if (Regex.match(S))
154 Contains = Positive;
155
156 if (NextGlob)
157 Contains = NextGlob->contains(S, Contains);
158 return Contains;
159}
160
Alexander Kornienko61803372017-05-18 01:13:51 +0000161class ClangTidyContext::CachedGlobList {
162public:
163 CachedGlobList(StringRef Globs) : Globs(Globs) {}
164
165 bool contains(StringRef S) {
166 switch (auto &Result = Cache[S]) {
167 case Yes: return true;
168 case No: return false;
169 case None:
170 Result = Globs.contains(S) ? Yes : No;
171 return Result == Yes;
172 }
Simon Pilgrim8d81c222017-05-18 10:48:23 +0000173 llvm_unreachable("invalid enum");
Alexander Kornienko61803372017-05-18 01:13:51 +0000174 }
175
176private:
177 GlobList Globs;
178 enum Tristate { None, Yes, No };
179 llvm::StringMap<Tristate> Cache;
180};
181
Alexander Kornienkod0488d42017-05-09 14:56:28 +0000182ClangTidyContext::ClangTidyContext(
183 std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider)
184 : DiagEngine(nullptr), OptionsProvider(std::move(OptionsProvider)),
185 Profile(nullptr) {
186 // Before the first translation unit we can get errors related to command-line
187 // parsing, use empty string for the file name in this case.
188 setCurrentFile("");
189}
190
Alexander Kornienko61803372017-05-18 01:13:51 +0000191ClangTidyContext::~ClangTidyContext() = default;
192
Alexander Kornienkod0488d42017-05-09 14:56:28 +0000193DiagnosticBuilder ClangTidyContext::diag(
194 StringRef CheckName, SourceLocation Loc, StringRef Description,
195 DiagnosticIDs::Level Level /* = DiagnosticIDs::Warning*/) {
196 assert(Loc.isValid());
197 unsigned ID = DiagEngine->getDiagnosticIDs()->getCustomDiagID(
198 Level, (Description + " [" + CheckName + "]").str());
199 CheckNamesByDiagnosticID.try_emplace(ID, CheckName);
200 return DiagEngine->Report(Loc, ID);
201}
202
203void ClangTidyContext::setDiagnosticsEngine(DiagnosticsEngine *Engine) {
204 DiagEngine = Engine;
205}
206
207void ClangTidyContext::setSourceManager(SourceManager *SourceMgr) {
208 DiagEngine->setSourceManager(SourceMgr);
209}
210
211void ClangTidyContext::setCurrentFile(StringRef File) {
212 CurrentFile = File;
213 CurrentOptions = getOptionsForFile(CurrentFile);
Alexander Kornienko61803372017-05-18 01:13:51 +0000214 CheckFilter = llvm::make_unique<CachedGlobList>(*getOptions().Checks);
215 WarningAsErrorFilter =
216 llvm::make_unique<CachedGlobList>(*getOptions().WarningsAsErrors);
Alexander Kornienkod0488d42017-05-09 14:56:28 +0000217}
218
219void ClangTidyContext::setASTContext(ASTContext *Context) {
220 DiagEngine->SetArgToStringFn(&FormatASTNodeDiagnosticArgument, Context);
221 LangOpts = Context->getLangOpts();
222}
223
224const ClangTidyGlobalOptions &ClangTidyContext::getGlobalOptions() const {
225 return OptionsProvider->getGlobalOptions();
226}
227
228const ClangTidyOptions &ClangTidyContext::getOptions() const {
229 return CurrentOptions;
230}
231
232ClangTidyOptions ClangTidyContext::getOptionsForFile(StringRef File) const {
233 // Merge options on top of getDefaults() as a safeguard against options with
234 // unset values.
235 return ClangTidyOptions::getDefaults().mergeWith(
236 OptionsProvider->getOptions(File));
237}
238
239void ClangTidyContext::setCheckProfileData(ProfileData *P) { Profile = P; }
240
Alexander Kornienko21375182017-05-17 14:39:47 +0000241bool ClangTidyContext::isCheckEnabled(StringRef CheckName) const {
Alexander Kornienkod0488d42017-05-09 14:56:28 +0000242 assert(CheckFilter != nullptr);
Alexander Kornienko21375182017-05-17 14:39:47 +0000243 return CheckFilter->contains(CheckName);
Alexander Kornienkod0488d42017-05-09 14:56:28 +0000244}
245
Alexander Kornienko21375182017-05-17 14:39:47 +0000246bool ClangTidyContext::treatAsError(StringRef CheckName) const {
Alexander Kornienkod0488d42017-05-09 14:56:28 +0000247 assert(WarningAsErrorFilter != nullptr);
Alexander Kornienko21375182017-05-17 14:39:47 +0000248 return WarningAsErrorFilter->contains(CheckName);
Alexander Kornienkod0488d42017-05-09 14:56:28 +0000249}
250
251/// \brief Store a \c ClangTidyError.
252void ClangTidyContext::storeError(const ClangTidyError &Error) {
253 Errors.push_back(Error);
254}
255
256StringRef ClangTidyContext::getCheckName(unsigned DiagnosticID) const {
257 llvm::DenseMap<unsigned, std::string>::const_iterator I =
258 CheckNamesByDiagnosticID.find(DiagnosticID);
259 if (I != CheckNamesByDiagnosticID.end())
260 return I->second;
261 return "";
262}
263
Alexander Kornienko9660d4d2017-05-09 15:10:26 +0000264ClangTidyDiagnosticConsumer::ClangTidyDiagnosticConsumer(
265 ClangTidyContext &Ctx, bool RemoveIncompatibleErrors)
266 : Context(Ctx), RemoveIncompatibleErrors(RemoveIncompatibleErrors),
267 LastErrorRelatesToUserCode(false), LastErrorPassesLineFilter(false),
268 LastErrorWasIgnored(false) {
Alexander Kornienkod0488d42017-05-09 14:56:28 +0000269 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
Alexander Kornienko61803372017-05-18 01:13:51 +0000270 Diags = llvm::make_unique<DiagnosticsEngine>(
Alexander Kornienkod0488d42017-05-09 14:56:28 +0000271 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), &*DiagOpts, this,
Alexander Kornienko61803372017-05-18 01:13:51 +0000272 /*ShouldOwnClient=*/false);
Alexander Kornienkod0488d42017-05-09 14:56:28 +0000273 Context.setDiagnosticsEngine(Diags.get());
274}
275
276void ClangTidyDiagnosticConsumer::finalizeLastError() {
277 if (!Errors.empty()) {
278 ClangTidyError &Error = Errors.back();
Alexander Kornienko21375182017-05-17 14:39:47 +0000279 if (!Context.isCheckEnabled(Error.DiagnosticName) &&
Alexander Kornienkod0488d42017-05-09 14:56:28 +0000280 Error.DiagLevel != ClangTidyError::Error) {
281 ++Context.Stats.ErrorsIgnoredCheckFilter;
282 Errors.pop_back();
283 } else if (!LastErrorRelatesToUserCode) {
284 ++Context.Stats.ErrorsIgnoredNonUserCode;
285 Errors.pop_back();
286 } else if (!LastErrorPassesLineFilter) {
287 ++Context.Stats.ErrorsIgnoredLineFilter;
288 Errors.pop_back();
289 } else {
290 ++Context.Stats.ErrorsDisplayed;
291 }
292 }
293 LastErrorRelatesToUserCode = false;
294 LastErrorPassesLineFilter = false;
295}
296
297static bool LineIsMarkedWithNOLINT(SourceManager &SM, SourceLocation Loc) {
298 bool Invalid;
299 const char *CharacterData = SM.getCharacterData(Loc, &Invalid);
300 if (Invalid)
301 return false;
302
303 // Check if there's a NOLINT on this line.
304 const char *P = CharacterData;
305 while (*P != '\0' && *P != '\r' && *P != '\n')
306 ++P;
307 StringRef RestOfLine(CharacterData, P - CharacterData + 1);
308 // FIXME: Handle /\bNOLINT\b(\([^)]*\))?/ as cpplint.py does.
309 if (RestOfLine.find("NOLINT") != StringRef::npos)
310 return true;
311
312 // Check if there's a NOLINTNEXTLINE on the previous line.
313 const char *BufBegin =
314 SM.getCharacterData(SM.getLocForStartOfFile(SM.getFileID(Loc)), &Invalid);
315 if (Invalid || P == BufBegin)
316 return false;
317
318 // Scan backwards over the current line.
319 P = CharacterData;
320 while (P != BufBegin && *P != '\n')
321 --P;
322
323 // If we reached the begin of the file there is no line before it.
324 if (P == BufBegin)
325 return false;
326
327 // Skip over the newline.
328 --P;
329 const char *LineEnd = P;
330
331 // Now we're on the previous line. Skip to the beginning of it.
332 while (P != BufBegin && *P != '\n')
333 --P;
334
335 RestOfLine = StringRef(P, LineEnd - P + 1);
336 if (RestOfLine.find("NOLINTNEXTLINE") != StringRef::npos)
337 return true;
338
339 return false;
340}
341
342static bool LineIsMarkedWithNOLINTinMacro(SourceManager &SM,
343 SourceLocation Loc) {
344 while (true) {
345 if (LineIsMarkedWithNOLINT(SM, Loc))
346 return true;
347 if (!Loc.isMacroID())
348 return false;
349 Loc = SM.getImmediateExpansionRange(Loc).first;
350 }
351 return false;
352}
353
354void ClangTidyDiagnosticConsumer::HandleDiagnostic(
355 DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
356 if (LastErrorWasIgnored && DiagLevel == DiagnosticsEngine::Note)
357 return;
358
359 if (Info.getLocation().isValid() && DiagLevel != DiagnosticsEngine::Error &&
360 DiagLevel != DiagnosticsEngine::Fatal &&
361 LineIsMarkedWithNOLINTinMacro(Diags->getSourceManager(),
362 Info.getLocation())) {
363 ++Context.Stats.ErrorsIgnoredNOLINT;
364 // Ignored a warning, should ignore related notes as well
365 LastErrorWasIgnored = true;
366 return;
367 }
368
369 LastErrorWasIgnored = false;
370 // Count warnings/errors.
371 DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
372
373 if (DiagLevel == DiagnosticsEngine::Note) {
374 assert(!Errors.empty() &&
375 "A diagnostic note can only be appended to a message.");
376 } else {
377 finalizeLastError();
378 StringRef WarningOption =
379 Context.DiagEngine->getDiagnosticIDs()->getWarningOptionForDiag(
380 Info.getID());
381 std::string CheckName = !WarningOption.empty()
382 ? ("clang-diagnostic-" + WarningOption).str()
383 : Context.getCheckName(Info.getID()).str();
384
385 if (CheckName.empty()) {
386 // This is a compiler diagnostic without a warning option. Assign check
387 // name based on its level.
388 switch (DiagLevel) {
389 case DiagnosticsEngine::Error:
390 case DiagnosticsEngine::Fatal:
391 CheckName = "clang-diagnostic-error";
392 break;
393 case DiagnosticsEngine::Warning:
394 CheckName = "clang-diagnostic-warning";
395 break;
396 default:
397 CheckName = "clang-diagnostic-unknown";
398 break;
399 }
400 }
401
402 ClangTidyError::Level Level = ClangTidyError::Warning;
403 if (DiagLevel == DiagnosticsEngine::Error ||
404 DiagLevel == DiagnosticsEngine::Fatal) {
405 // Force reporting of Clang errors regardless of filters and non-user
406 // code.
407 Level = ClangTidyError::Error;
408 LastErrorRelatesToUserCode = true;
409 LastErrorPassesLineFilter = true;
410 }
Alexander Kornienko21375182017-05-17 14:39:47 +0000411 bool IsWarningAsError = DiagLevel == DiagnosticsEngine::Warning &&
412 Context.treatAsError(CheckName);
Alexander Kornienkod0488d42017-05-09 14:56:28 +0000413 Errors.emplace_back(CheckName, Level, Context.getCurrentBuildDirectory(),
414 IsWarningAsError);
415 }
416
417 ClangTidyDiagnosticRenderer Converter(
418 Context.getLangOpts(), &Context.DiagEngine->getDiagnosticOptions(),
419 Errors.back());
420 SmallString<100> Message;
421 Info.FormatDiagnostic(Message);
422 SourceManager *Sources = nullptr;
423 if (Info.hasSourceManager())
424 Sources = &Info.getSourceManager();
425 Converter.emitDiagnostic(Info.getLocation(), DiagLevel, Message,
426 Info.getRanges(), Info.getFixItHints(), Sources);
427
428 checkFilters(Info.getLocation());
429}
430
431bool ClangTidyDiagnosticConsumer::passesLineFilter(StringRef FileName,
432 unsigned LineNumber) const {
433 if (Context.getGlobalOptions().LineFilter.empty())
434 return true;
435 for (const FileFilter &Filter : Context.getGlobalOptions().LineFilter) {
436 if (FileName.endswith(Filter.Name)) {
437 if (Filter.LineRanges.empty())
438 return true;
439 for (const FileFilter::LineRange &Range : Filter.LineRanges) {
440 if (Range.first <= LineNumber && LineNumber <= Range.second)
441 return true;
442 }
443 return false;
444 }
445 }
446 return false;
447}
448
449void ClangTidyDiagnosticConsumer::checkFilters(SourceLocation Location) {
450 // Invalid location may mean a diagnostic in a command line, don't skip these.
451 if (!Location.isValid()) {
452 LastErrorRelatesToUserCode = true;
453 LastErrorPassesLineFilter = true;
454 return;
455 }
456
457 const SourceManager &Sources = Diags->getSourceManager();
458 if (!*Context.getOptions().SystemHeaders &&
459 Sources.isInSystemHeader(Location))
460 return;
461
462 // FIXME: We start with a conservative approach here, but the actual type of
463 // location needed depends on the check (in particular, where this check wants
464 // to apply fixes).
465 FileID FID = Sources.getDecomposedExpansionLoc(Location).first;
466 const FileEntry *File = Sources.getFileEntryForID(FID);
467
468 // -DMACRO definitions on the command line have locations in a virtual buffer
469 // that doesn't have a FileEntry. Don't skip these as well.
470 if (!File) {
471 LastErrorRelatesToUserCode = true;
472 LastErrorPassesLineFilter = true;
473 return;
474 }
475
476 StringRef FileName(File->getName());
477 LastErrorRelatesToUserCode = LastErrorRelatesToUserCode ||
478 Sources.isInMainFile(Location) ||
479 getHeaderFilter()->match(FileName);
480
481 unsigned LineNumber = Sources.getExpansionLineNumber(Location);
482 LastErrorPassesLineFilter =
483 LastErrorPassesLineFilter || passesLineFilter(FileName, LineNumber);
484}
485
486llvm::Regex *ClangTidyDiagnosticConsumer::getHeaderFilter() {
487 if (!HeaderFilter)
Alexander Kornienko61803372017-05-18 01:13:51 +0000488 HeaderFilter =
489 llvm::make_unique<llvm::Regex>(*Context.getOptions().HeaderFilterRegex);
Alexander Kornienkod0488d42017-05-09 14:56:28 +0000490 return HeaderFilter.get();
491}
492
493void ClangTidyDiagnosticConsumer::removeIncompatibleErrors(
494 SmallVectorImpl<ClangTidyError> &Errors) const {
495 // Each error is modelled as the set of intervals in which it applies
496 // replacements. To detect overlapping replacements, we use a sweep line
497 // algorithm over these sets of intervals.
498 // An event here consists of the opening or closing of an interval. During the
499 // process, we maintain a counter with the amount of open intervals. If we
500 // find an endpoint of an interval and this counter is different from 0, it
501 // means that this interval overlaps with another one, so we set it as
502 // inapplicable.
503 struct Event {
504 // An event can be either the begin or the end of an interval.
505 enum EventType {
506 ET_Begin = 1,
507 ET_End = -1,
508 };
509
510 Event(unsigned Begin, unsigned End, EventType Type, unsigned ErrorId,
511 unsigned ErrorSize)
512 : Type(Type), ErrorId(ErrorId) {
513 // The events are going to be sorted by their position. In case of draw:
514 //
515 // * If an interval ends at the same position at which other interval
516 // begins, this is not an overlapping, so we want to remove the ending
517 // interval before adding the starting one: end events have higher
518 // priority than begin events.
519 //
520 // * If we have several begin points at the same position, we will mark as
521 // inapplicable the ones that we process later, so the first one has to
522 // be the one with the latest end point, because this one will contain
523 // all the other intervals. For the same reason, if we have several end
524 // points in the same position, the last one has to be the one with the
525 // earliest begin point. In both cases, we sort non-increasingly by the
526 // position of the complementary.
527 //
528 // * In case of two equal intervals, the one whose error is bigger can
529 // potentially contain the other one, so we want to process its begin
530 // points before and its end points later.
531 //
532 // * Finally, if we have two equal intervals whose errors have the same
533 // size, none of them will be strictly contained inside the other.
534 // Sorting by ErrorId will guarantee that the begin point of the first
535 // one will be processed before, disallowing the second one, and the
536 // end point of the first one will also be processed before,
537 // disallowing the first one.
538 if (Type == ET_Begin)
539 Priority = std::make_tuple(Begin, Type, -End, -ErrorSize, ErrorId);
540 else
541 Priority = std::make_tuple(End, Type, -Begin, ErrorSize, ErrorId);
542 }
543
544 bool operator<(const Event &Other) const {
545 return Priority < Other.Priority;
546 }
547
548 // Determines if this event is the begin or the end of an interval.
549 EventType Type;
550 // The index of the error to which the interval that generated this event
551 // belongs.
552 unsigned ErrorId;
553 // The events will be sorted based on this field.
554 std::tuple<unsigned, EventType, int, int, unsigned> Priority;
555 };
556
557 // Compute error sizes.
558 std::vector<int> Sizes;
559 for (const auto &Error : Errors) {
560 int Size = 0;
561 for (const auto &FileAndReplaces : Error.Fix) {
562 for (const auto &Replace : FileAndReplaces.second)
563 Size += Replace.getLength();
564 }
565 Sizes.push_back(Size);
566 }
567
568 // Build events from error intervals.
569 std::map<std::string, std::vector<Event>> FileEvents;
570 for (unsigned I = 0; I < Errors.size(); ++I) {
571 for (const auto &FileAndReplace : Errors[I].Fix) {
572 for (const auto &Replace : FileAndReplace.second) {
573 unsigned Begin = Replace.getOffset();
574 unsigned End = Begin + Replace.getLength();
575 const std::string &FilePath = Replace.getFilePath();
576 // FIXME: Handle empty intervals, such as those from insertions.
577 if (Begin == End)
578 continue;
579 auto &Events = FileEvents[FilePath];
580 Events.emplace_back(Begin, End, Event::ET_Begin, I, Sizes[I]);
581 Events.emplace_back(Begin, End, Event::ET_End, I, Sizes[I]);
582 }
583 }
584 }
585
586 std::vector<bool> Apply(Errors.size(), true);
587 for (auto &FileAndEvents : FileEvents) {
588 std::vector<Event> &Events = FileAndEvents.second;
589 // Sweep.
590 std::sort(Events.begin(), Events.end());
591 int OpenIntervals = 0;
592 for (const auto &Event : Events) {
593 if (Event.Type == Event::ET_End)
594 --OpenIntervals;
595 // This has to be checked after removing the interval from the count if it
596 // is an end event, or before adding it if it is a begin event.
597 if (OpenIntervals != 0)
598 Apply[Event.ErrorId] = false;
599 if (Event.Type == Event::ET_Begin)
600 ++OpenIntervals;
601 }
602 assert(OpenIntervals == 0 && "Amount of begin/end points doesn't match");
603 }
604
605 for (unsigned I = 0; I < Errors.size(); ++I) {
606 if (!Apply[I]) {
607 Errors[I].Fix.clear();
608 Errors[I].Notes.emplace_back(
609 "this fix will not be applied because it overlaps with another fix");
610 }
611 }
612}
613
614namespace {
615struct LessClangTidyError {
616 bool operator()(const ClangTidyError &LHS, const ClangTidyError &RHS) const {
617 const tooling::DiagnosticMessage &M1 = LHS.Message;
618 const tooling::DiagnosticMessage &M2 = RHS.Message;
619
620 return std::tie(M1.FilePath, M1.FileOffset, M1.Message) <
621 std::tie(M2.FilePath, M2.FileOffset, M2.Message);
622 }
623};
624struct EqualClangTidyError {
625 bool operator()(const ClangTidyError &LHS, const ClangTidyError &RHS) const {
626 LessClangTidyError Less;
627 return !Less(LHS, RHS) && !Less(RHS, LHS);
628 }
629};
630} // end anonymous namespace
631
632// Flushes the internal diagnostics buffer to the ClangTidyContext.
633void ClangTidyDiagnosticConsumer::finish() {
634 finalizeLastError();
635
636 std::sort(Errors.begin(), Errors.end(), LessClangTidyError());
637 Errors.erase(std::unique(Errors.begin(), Errors.end(), EqualClangTidyError()),
638 Errors.end());
Alexander Kornienko9660d4d2017-05-09 15:10:26 +0000639
640 if (RemoveIncompatibleErrors)
641 removeIncompatibleErrors(Errors);
Alexander Kornienkod0488d42017-05-09 14:56:28 +0000642
643 for (const ClangTidyError &Error : Errors)
644 Context.storeError(Error);
645 Errors.clear();
646}