This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Add basic conflict detection for the rename.
ClosedPublic

Authored by hokein on Oct 20 2020, 7:02 AM.

Details

Summary

With this patch, we reject the rename if the new name would conflict with
any other decls in the decl context of the renamed decl.

Diff Detail

Event Timeline

hokein created this revision.Oct 20 2020, 7:02 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 20 2020, 7:02 AM
hokein requested review of this revision.Oct 20 2020, 7:02 AM
sammccall added inline comments.Nov 2 2020, 6:07 AM
clang-tools-extra/clangd/refactor/Rename.cpp
263

explain this list somewhat?
e.g. these are the declcontexts which form a namespace where conflicts can occur?
and why function doesn't belong here

263

I think you want to walk up DC while isTransparentContext()

315

Can you explain this a bit more (e.g. why?)

My guess is given:

void foo() {
  int bar;
}

foo is a DC but bar is not declared directly within it (rather within the CompoundStmt that is its body), therefore will not be looked up.

In which case an explanation like "Note that the enclosing DeclContext may not be the enclosing scope, particularly for function-local declarations, so this has both false positives and negatives." might help.

hokein updated this revision to Diff 302928.Nov 4 2020, 11:56 AM
hokein marked 2 inline comments as done.

address comments.

clang-tools-extra/clangd/refactor/Rename.cpp
263

yeah, that's a good point. added.

315

Done. This comment is about why we exclude functionDecl, moved it to the lookup function there.

sammccall accepted this revision.Nov 6 2020, 4:47 AM

Thanks! Just style nits.

clang-tools-extra/clangd/refactor/Rename.cpp
266

this comment is missing the *reason*.

FunctionDecl is a good example, but the fundamental reason is again not given.
Maybe change that paragraph to:

Notably, FunctionDecl is excluded, because local variables are not scoped to the function, but rather to the CompoundStmt that is its body.
Lookup will not find function-local variables.
268

confidence in what?

I think the condition is: don't have any subscopes that are neither DeclContexts nor transparent.

320

This is still a bit confusing: *here* you seem to decide which scope to look up in, but then inside the lookup() function you may change your mind.
I'd suggest moving the getDeclContext() inside the function and renaming the function to "findSiblingWithName" or something.

Also while early-exit is often good, I find early-exit in the *success* case confusing.
Consider:

// Name conflict detection.
// Function conflicts are subtle (overloading), so ignore them.
if (RenameDecl.getKind() != Decl::Function) {
  if (lookupSiblingWithName(ASTCtx, RenameDecl, NewName))
    return InvalidName{...};
}
return llvm::None;
This revision is now accepted and ready to land.Nov 6 2020, 4:47 AM
hokein updated this revision to Diff 303768.Nov 9 2020, 12:51 AM
hokein marked 3 inline comments as done.

address review comments.

This revision was landed with ongoing or failed builds.Nov 9 2020, 11:52 PM
This revision was automatically updated to reflect the committed changes.
thakis added a subscriber: thakis.Nov 10 2020, 3:25 AM

This breaks compillation on windows when using clang as compiler: http://45.33.8.238/win/27605/step_4.txt

This breaks compillation on windows when using clang as compiler: http://45.33.8.238/win/27605/step_4.txt

sorry, looking at it now.