[PATCH] D95536: [clang][sema] Note decl location on missing member

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 27 08:37:49 PST 2021


tbaeder created this revision.
tbaeder added reviewers: aaron.ballman, rsmith, rjmccall, rtrieu.
Herald added subscribers: lxfind, jdoerfert.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Hi,

when trying to access a member of a namespace/class/struct/enum, clang currently just prints that the given namespace does not have a member with the given name, for example:

  ./enum.cpp:12:7: error: no member named 'b' in 'Foo'
    foo.b = 10;
    ~~~ ^
  1 error generated.

However, for programmers it is usually also important to know where the namespace has been declared, e.g. in the example above it would be useful to know where `foo`'s type has been declared. The following patch adds support for this by simply printing a note:

  ./enum.cpp:12:7: error: no member named 'b' in 'Foo'
    foo.b = 10;
    ~~~ ^
  ./enum.cpp:3:7: note: 'Foo' declared here
  class Foo {
        ^~~
  1 error generated.

That is of course a very simple example, but I think this adds value in real-world scenarios.

The patch is unfortunately all over the place and mostly updating test cases with the additional expected note.

In addition to the usual review I'd like opinions on the following open questions:

- There are now three places where the patch adds the exact same code, would a helper function (in `Sema`?) be better and if so, where is the best place for it?
- The notes are pretty clunky to read when the namespace is created by a lambda: " note: '(lambda at enum.cpp:41:12)' declared here" - should lambdas simply be ignored (how to do that?) or should I handle them differently?
- The patch only emits the note if clang does not output a typo correction. I thought it might not be as useful in this case since it would clutter the output and chances are that the typo fix _is_ the right thing to show. Opinions on this?

Thanks,
Timm


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D95536

Files:
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaExprMember.cpp
  clang/test/CXX/basic/basic.lookup/basic.lookup.classref/p1-cxx11.cpp
  clang/test/CXX/basic/basic.lookup/basic.lookup.classref/p1.cpp
  clang/test/CXX/class.access/class.friend/p1.cpp
  clang/test/CXX/class/class.mfct/class.mfct.non-static/p3.cpp
  clang/test/CXX/drs/dr1xx.cpp
  clang/test/CXX/drs/dr21xx.cpp
  clang/test/CXX/drs/dr3xx.cpp
  clang/test/CXX/drs/dr6xx.cpp
  clang/test/CXX/expr/expr.prim/expr.prim.lambda/p11-1y.cpp
  clang/test/CXX/module/module.interface/p2.cpp
  clang/test/CXX/special/class.copy/p28-cxx11.cpp
  clang/test/CXX/special/class.dtor/p10-0x.cpp
  clang/test/CXX/temp/temp.decls/temp.class/temp.mem.enum/p1.cpp
  clang/test/CXX/temp/temp.res/temp.local/p3.cpp
  clang/test/CXX/temp/temp.res/temp.local/p9.cpp
  clang/test/CXX/temp/temp.spec/temp.expl.spec/p19.cpp
  clang/test/Misc/diag-template.cpp
  clang/test/Modules/Inputs/redecl_namespaces_left.h
  clang/test/Modules/cxx-templates.cpp
  clang/test/Modules/module-private.cpp
  clang/test/Modules/redecl-namespaces.mm
  clang/test/OpenMP/target_map_messages.cpp
  clang/test/OpenMP/target_teams_map_messages.cpp
  clang/test/PCH/decl-attrs.cpp
  clang/test/Parser/recovery.c
  clang/test/Parser/recovery.cpp
  clang/test/Sema/MicrosoftExtensions.c
  clang/test/Sema/anonymous-struct-union.c
  clang/test/Sema/typo-correction-ambiguity.cpp
  clang/test/Sema/typo-correction-no-hang.cpp
  clang/test/Sema/typo-correction-recursive.cpp
  clang/test/SemaCXX/MicrosoftExtensions.cpp
  clang/test/SemaCXX/anonymous-union.cpp
  clang/test/SemaCXX/attr-cleanup.cpp
  clang/test/SemaCXX/co_await-range-for.cpp
  clang/test/SemaCXX/constructor-initializer.cpp
  clang/test/SemaCXX/conversion-function.cpp
  clang/test/SemaCXX/coroutines.cpp
  clang/test/SemaCXX/cxx1y-deduced-return-type.cpp
  clang/test/SemaCXX/cxx1y-generic-lambdas.cpp
  clang/test/SemaCXX/cxx1y-variable-templates_in_class.cpp
  clang/test/SemaCXX/friend.cpp
  clang/test/SemaCXX/invalid-member-expr.cpp
  clang/test/SemaCXX/member-operator-expr.cpp
  clang/test/SemaCXX/missing-members.cpp
  clang/test/SemaCXX/nested-name-spec.cpp
  clang/test/SemaCXX/pseudo-destructors.cpp
  clang/test/SemaCXX/qualified-id-lookup.cpp
  clang/test/SemaCXX/typo-correction-delayed.cpp
  clang/test/SemaCXX/typo-correction.cpp
  clang/test/SemaCXX/unknown-type-name.cpp
  clang/test/SemaObjCXX/deduction.mm
  clang/test/SemaObjCXX/interface-return-type.mm
  clang/test/SemaTemplate/attributes.cpp
  clang/test/SemaTemplate/cxx1z-using-declaration.cpp
  clang/test/SemaTemplate/dependent-base-classes.cpp
  clang/test/SemaTemplate/instantiate-method.cpp
  clang/test/SemaTemplate/instantiate-non-dependent-types.cpp
  clang/test/SemaTemplate/member-access-ambig.cpp
  clang/test/SemaTemplate/member-access-expr.cpp
  clang/test/SemaTemplate/ms-lookup-template-base-classes.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95536.319588.patch
Type: text/x-patch
Size: 45413 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210127/cfe041db/attachment-0001.bin>


More information about the cfe-commits mailing list