Skip to content

Commit

Permalink
[clangd] Suppress unwritten scopes when expanding auto.
Browse files Browse the repository at this point in the history
Summary: otherwise the replacement will break the code.

Reviewers: sammccall

Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D64627

llvm-svn: 366446
  • Loading branch information
hokein committed Jul 18, 2019
1 parent f286fa3 commit ac958c2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions clang-tools-extra/clangd/AST.cpp
Expand Up @@ -192,6 +192,7 @@ std::string shortenNamespace(const llvm::StringRef OriginalName,

std::string printType(const QualType QT, const DeclContext & Context){
PrintingPolicy PP(Context.getParentASTContext().getPrintingPolicy());
PP.SuppressUnwrittenScope = 1;
PP.SuppressTagKeyword = 1;
return shortenNamespace(
QT.getAsString(PP),
Expand Down
3 changes: 2 additions & 1 deletion clang-tools-extra/clangd/AST.h
Expand Up @@ -67,7 +67,8 @@ llvm::Optional<SymbolID> getSymbolID(const IdentifierInfo &II,
const MacroInfo *MI,
const SourceManager &SM);

/// Returns a QualType as string.
/// Returns a QualType as string. The result doesn't contain unwritten scopes
/// like annoymous/inline namespace.
std::string printType(const QualType QT, const DeclContext & Context);

/// Try to shorten the OriginalName by removing namespaces from the left of
Expand Down
14 changes: 14 additions & 0 deletions clang-tools-extra/clangd/unittests/TweakTests.cpp
Expand Up @@ -663,6 +663,20 @@ TEST(TweakTest, ExpandAutoType) {
const char * x = "test";
)cpp";
checkTransform(ID, Input, Output);

Input = R"cpp(
namespace {
class Foo {};
}
au^to f = Foo();
)cpp";
Output = R"cpp(
namespace {
class Foo {};
}
Foo f = Foo();
)cpp";
checkTransform(ID, Input, Output);
}

} // namespace
Expand Down

0 comments on commit ac958c2

Please sign in to comment.