[PATCH] D127251: [Lex] Fix `fixits` for typo-corrections of preprocessing directives within skipped blocks

Argyrios Kyrtzidis via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 7 13:41:30 PDT 2022


akyrtzi created this revision.
Herald added a project: All.
akyrtzi requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

The `EndLoc` parameter was always unset so no fixit was emitted. But it is also unnecessary for determining the range so we can remove it.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D127251

Files:
  clang/include/clang/Lex/Preprocessor.h
  clang/lib/Lex/PPDirectives.cpp
  clang/test/Preprocessor/suggest-typoed-directive.c


Index: clang/test/Preprocessor/suggest-typoed-directive.c
===================================================================
--- clang/test/Preprocessor/suggest-typoed-directive.c
+++ clang/test/Preprocessor/suggest-typoed-directive.c
@@ -1,6 +1,7 @@
 // RUN: %clang_cc1 -fsyntax-only -verify=pre-c2x-cpp2b %s
 // RUN: %clang_cc1 -std=c2x -fsyntax-only -verify=c2x-cpp2b %s
 // RUN: %clang_cc1 -x c++ -std=c++2b -fsyntax-only -verify=c2x-cpp2b %s
+// RUN: %clang_cc1 -x c++ -std=c++2b -fsyntax-only %s -fdiagnostics-parseable-fixits 2>&1 | FileCheck %s
 
 // id:        pre-c2x-cpp2b-warning at +12 {{invalid preprocessing directive, did you mean '#if'?}}
 // ifd:       pre-c2x-cpp2b-warning at +12 {{invalid preprocessing directive, did you mean '#if'?}}
@@ -38,6 +39,18 @@
 // els:       c2x-cpp2b-warning at -12 {{invalid preprocessing directive, did you mean '#else'?}}
 // endi:      c2x-cpp2b-warning at -12 {{invalid preprocessing directive, did you mean '#endif'?}}
 
+// CHECK: fix-it:{{.*}}:{[[@LINE-24]]:2-[[@LINE-24]]:4}:"#if"
+// CHECK: fix-it:{{.*}}:{[[@LINE-24]]:2-[[@LINE-24]]:5}:"#if"
+// CHECK: fix-it:{{.*}}:{[[@LINE-24]]:2-[[@LINE-24]]:6}:"#ifdef"
+// CHECK: fix-it:{{.*}}:{[[@LINE-24]]:2-[[@LINE-24]]:5}:"#elif"
+// CHECK: fix-it:{{.*}}:{[[@LINE-24]]:2-[[@LINE-24]]:7}:"#elif"
+// CHECK: fix-it:{{.*}}:{[[@LINE-24]]:2-[[@LINE-24]]:8}:"#elif"
+// CHECK: fix-it:{{.*}}:{[[@LINE-24]]:2-[[@LINE-24]]:9}:"#elifdef"
+// CHECK: fix-it:{{.*}}:{[[@LINE-24]]:2-[[@LINE-24]]:10}:"#elifdef"
+// CHECK: fix-it:{{.*}}:{[[@LINE-24]]:2-[[@LINE-24]]:11}:"#elifndef"
+// CHECK: fix-it:{{.*}}:{[[@LINE-24]]:2-[[@LINE-24]]:5}:"#else"
+// CHECK: fix-it:{{.*}}:{[[@LINE-24]]:2-[[@LINE-24]]:6}:"#endif"
+
 #ifdef UNDEFINED
 #i // no diagnostic
 #endif
Index: clang/lib/Lex/PPDirectives.cpp
===================================================================
--- clang/lib/Lex/PPDirectives.cpp
+++ clang/lib/Lex/PPDirectives.cpp
@@ -444,8 +444,7 @@
 }
 
 void Preprocessor::SuggestTypoedDirective(const Token &Tok,
-                                          StringRef Directive,
-                                          const SourceLocation &EndLoc) const {
+                                          StringRef Directive) const {
   // If this is a `.S` file, treat unknown # directives as non-preprocessor
   // directives.
   if (getLangOpts().AsmPreprocessor) return;
@@ -457,8 +456,9 @@
     Candidates.insert(Candidates.end(), {"elifdef", "elifndef"});
 
   if (Optional<StringRef> Sugg = findSimilarStr(Directive, Candidates)) {
-    CharSourceRange DirectiveRange =
-        CharSourceRange::getCharRange(Tok.getLocation(), EndLoc);
+    CharSourceRange DirectiveRange = CharSourceRange::getCharRange(
+        Tok.getLocation(),
+        Tok.getLocation().getLocWithOffset(Directive.size()));
     std::string SuggValue = Sugg.getValue().str();
 
     auto Hint = FixItHint::CreateReplacement(DirectiveRange, "#" + SuggValue);
@@ -596,7 +596,7 @@
                                        /*foundnonskip*/false,
                                        /*foundelse*/false);
       } else {
-        SuggestTypoedDirective(Tok, Directive, endLoc);
+        SuggestTypoedDirective(Tok, Directive);
       }
     } else if (Directive[0] == 'e') {
       StringRef Sub = Directive.substr(1);
@@ -758,10 +758,10 @@
           }
         }
       } else {
-        SuggestTypoedDirective(Tok, Directive, endLoc);
+        SuggestTypoedDirective(Tok, Directive);
       }
     } else {
-      SuggestTypoedDirective(Tok, Directive, endLoc);
+      SuggestTypoedDirective(Tok, Directive);
     }
 
     CurPPLexer->ParsingPreprocessorDirective = false;
Index: clang/include/clang/Lex/Preprocessor.h
===================================================================
--- clang/include/clang/Lex/Preprocessor.h
+++ clang/include/clang/Lex/Preprocessor.h
@@ -2244,9 +2244,7 @@
   /// \param Tok - Token that represents the directive
   /// \param Directive - String reference for the directive name
   /// \param EndLoc - End location for fixit
-  void SuggestTypoedDirective(const Token &Tok,
-                              StringRef Directive,
-                              const SourceLocation &EndLoc) const;
+  void SuggestTypoedDirective(const Token &Tok, StringRef Directive) const;
 
   /// We just read a \#if or related directive and decided that the
   /// subsequent tokens are in the \#if'd out portion of the


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127251.434937.patch
Type: text/x-patch
Size: 4408 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220607/55c48d57/attachment-0001.bin>


More information about the cfe-commits mailing list