[include-fixer] Don't eat one token too many when replacing a block of includes.
authorBenjamin Kramer <benny.kra@googlemail.com>
Tue, 29 Nov 2016 15:15:26 +0000 (15:15 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Tue, 29 Nov 2016 15:15:26 +0000 (15:15 +0000)
SourceRanges are inclusive token ranges, this was trying to form an
exclusive char range.

llvm-svn: 288145

clang-tools-extra/include-fixer/IncludeFixer.cpp
clang-tools-extra/test/include-fixer/yamldb_plugin.cpp

index dd30ca6..cef6fa5 100644 (file)
@@ -136,10 +136,10 @@ static void addDiagnosticsForContext(TypoCorrection &Correction,
   const tooling::Replacement &Placed = *Reps->begin();
 
   auto Begin = StartOfFile.getLocWithOffset(Placed.getOffset());
-  auto End = Begin.getLocWithOffset(Placed.getLength());
+  auto End = Begin.getLocWithOffset(std::max(0, (int)Placed.getLength() - 1));
   PartialDiagnostic PD(DiagID, Ctx.getDiagAllocator());
   PD << Context.getHeaderInfos().front().Header
-     << FixItHint::CreateReplacement(SourceRange(Begin, End),
+     << FixItHint::CreateReplacement(CharSourceRange::getCharRange(Begin, End),
                                      Placed.getReplacementText());
   Correction.addExtraDiagnostic(std::move(PD));
 }
index 260b459..fa486cf 100644 (file)
@@ -8,7 +8,8 @@ unknown u;
 // CHECK: FIX-IT: Replace [3:1 - 3:4] with "foo"
 // CHECK: yamldb_plugin.cpp:3:1: note: Add '#include "foo.h"' to provide the missing declaration [clang-include-fixer]
 // CHECK: Number FIX-ITs = 1
-// CHECK: FIX-IT: Replace [3:1 - 3:4] with "#include "foo.h"
+// CHECK: FIX-IT: Insert "#include "foo.h"
+// CHECK: " at 3:1
 // CHECK: yamldb_plugin.cpp:4:1:
 // CHECK: error: unknown type name 'unknown'
 // CHECK: Number FIX-ITs = 0