[PATCH] D115567: [Sema] Add FixIt when a C++ out-of-line method has extra/missing const

Sam McCall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 15 05:31:16 PST 2021


This revision was automatically updated to reflect the committed changes.
sammccall marked an inline comment as done.
Closed by commit rGe7007b69d43b: [Sema] Add FixIt when a C++ out-of-line method has extra/missing const (authored by sammccall).

Changed prior to commit:
  https://reviews.llvm.org/D115567?vs=393646&id=394536#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D115567/new/

https://reviews.llvm.org/D115567

Files:
  clang/lib/Sema/SemaDecl.cpp
  clang/test/FixIt/member-mismatch.cpp


Index: clang/test/FixIt/member-mismatch.cpp
===================================================================
--- /dev/null
+++ clang/test/FixIt/member-mismatch.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -verify %s
+// RUN: not %clang_cc1 -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
+
+class Foo {
+  int get() const; // expected-note {{because it is const qualified}}
+  void set(int); // expected-note {{because it is not const qualified}}
+};
+
+// CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:15-[[@LINE+1]]:15}:" const"
+int Foo::get() {} // expected-error {{does not match any declaration}}
+// CHECK: fix-it:"{{.*}}":{[[@LINE+1]]:20-[[@LINE+1]]:26}:""
+void Foo::set(int) const {} // expected-error {{does not match any declaration}}
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -8504,7 +8504,14 @@
         << NewFD->getParamDecl(Idx - 1)->getType();
     } else if (FDisConst != NewFDisConst) {
       SemaRef.Diag(FD->getLocation(), diag::note_member_def_close_const_match)
-          << NewFDisConst << FD->getSourceRange().getEnd();
+          << NewFDisConst << FD->getSourceRange().getEnd()
+          << (NewFDisConst
+                  ? FixItHint::CreateRemoval(ExtraArgs.D.getFunctionTypeInfo()
+                                                 .getConstQualifierLoc())
+                  : FixItHint::CreateInsertion(ExtraArgs.D.getFunctionTypeInfo()
+                                                   .getRParenLoc()
+                                                   .getLocWithOffset(1),
+                                               " const"));
     } else
       SemaRef.Diag(FD->getLocation(),
                    IsMember ? diag::note_member_def_close_match


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115567.394536.patch
Type: text/x-patch
Size: 1814 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211215/a6f2ae9b/attachment.bin>


More information about the cfe-commits mailing list