Skip to content

Commit ac958c2

Browse files
committed
[clangd] Suppress unwritten scopes when expanding auto.
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
1 parent f286fa3 commit ac958c2

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

clang-tools-extra/clangd/AST.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ std::string shortenNamespace(const llvm::StringRef OriginalName,
192192

193193
std::string printType(const QualType QT, const DeclContext & Context){
194194
PrintingPolicy PP(Context.getParentASTContext().getPrintingPolicy());
195+
PP.SuppressUnwrittenScope = 1;
195196
PP.SuppressTagKeyword = 1;
196197
return shortenNamespace(
197198
QT.getAsString(PP),

clang-tools-extra/clangd/AST.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ llvm::Optional<SymbolID> getSymbolID(const IdentifierInfo &II,
6767
const MacroInfo *MI,
6868
const SourceManager &SM);
6969

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

7374
/// Try to shorten the OriginalName by removing namespaces from the left of

clang-tools-extra/clangd/unittests/TweakTests.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,20 @@ TEST(TweakTest, ExpandAutoType) {
663663
const char * x = "test";
664664
)cpp";
665665
checkTransform(ID, Input, Output);
666+
667+
Input = R"cpp(
668+
namespace {
669+
class Foo {};
670+
}
671+
au^to f = Foo();
672+
)cpp";
673+
Output = R"cpp(
674+
namespace {
675+
class Foo {};
676+
}
677+
Foo f = Foo();
678+
)cpp";
679+
checkTransform(ID, Input, Output);
666680
}
667681

668682
} // namespace

0 commit comments

Comments
 (0)