[llvm-branch-commits] [clang] d462aa5 - [clang] Fix a nullptr dereference bug on invalid code

2021-01-25 Thread Adam Czachorowski via llvm-branch-commits

Author: Adam Czachorowski
Date: 2021-01-25T15:02:25+01:00
New Revision: d462aa5a619ab9fdf8b024e48c19bc8820fe8781

URL: 
https://github.com/llvm/llvm-project/commit/d462aa5a619ab9fdf8b024e48c19bc8820fe8781
DIFF: 
https://github.com/llvm/llvm-project/commit/d462aa5a619ab9fdf8b024e48c19bc8820fe8781.diff

LOG: [clang] Fix a nullptr dereference bug on invalid code

When working with invalid code, we would try to dereference a nullptr
while deducing template arguments in some dependend code operating on a
lambda with invalid return type.

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

Added: 
clang/test/SemaCXX/subst-func-type-invalid-ret-type.cpp

Modified: 
clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Removed: 




diff  --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp 
b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index d3d6df5e0064..dc1e0ef60cac 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -4189,6 +4189,9 @@ TemplateDeclInstantiator::SubstFunctionType(FunctionDecl 
*D,
   for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumParams();
OldIdx != NumOldParams; ++OldIdx) {
 ParmVarDecl *OldParam = OldProtoLoc.getParam(OldIdx);
+if (!OldParam)
+  return nullptr;
+
 LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope;
 
 Optional NumArgumentsInExpansion;

diff  --git a/clang/test/SemaCXX/subst-func-type-invalid-ret-type.cpp 
b/clang/test/SemaCXX/subst-func-type-invalid-ret-type.cpp
new file mode 100644
index ..c783ff50
--- /dev/null
+++ b/clang/test/SemaCXX/subst-func-type-invalid-ret-type.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang -fsyntax-only -std=c++17 %s -Xclang -verify
+
+// The important part is that we do not crash.
+
+template T declval();
+
+template 
+auto Call(T x) -> decltype(declval()(0)) {} // expected-note{{candidate 
template ignored}}
+
+class Status {};
+
+void fun() {
+  // The Status() (instead of Status) here used to cause a crash.
+  Call([](auto x) -> Status() {}); // expected-error{{function cannot return 
function type 'Status ()}}
+  // expected-error@-1{{no matching function for call to 'Call'}}
+}



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] 0005438 - [clangd] Fix a crash when indexing invalid ObjC method declaration

2021-01-25 Thread Adam Czachorowski via llvm-branch-commits

Author: Adam Czachorowski
Date: 2021-01-25T15:43:11+01:00
New Revision: 00054382b95a9d95e2df6457e7fe1fca2323d287

URL: 
https://github.com/llvm/llvm-project/commit/00054382b95a9d95e2df6457e7fe1fca2323d287
DIFF: 
https://github.com/llvm/llvm-project/commit/00054382b95a9d95e2df6457e7fe1fca2323d287.diff

LOG: [clangd] Fix a crash when indexing invalid ObjC method declaration

This fix will make us not crash, but ideally we would handle this case
better.

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

Added: 


Modified: 
clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
clang/lib/Sema/SemaCodeComplete.cpp

Removed: 




diff  --git a/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp 
b/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
index 7e36cff7afa6..924cfd03cba7 100644
--- a/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
+++ b/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
@@ -1838,6 +1838,20 @@ TEST_F(SymbolCollectorTest, UndefOfModuleMacro) {
   EXPECT_THAT(TU.headerSymbols(), Not(Contains(QName("X";
 }
 
+TEST_F(SymbolCollectorTest, NoCrashOnObjCMethodCStyleParam) {
+  auto TU = TestTU::withCode(R"objc(
+@interface Foo
+- (void)fun:(bool)foo, bool bar;
+@end
+  )objc");
+  TU.ExtraArgs.push_back("-xobjective-c++");
+
+  TU.build();
+  // We mostly care about not crashing.
+  EXPECT_THAT(TU.headerSymbols(),
+  UnorderedElementsAre(QName("Foo"), QName("Foo::fun:")));
+}
+
 } // namespace
 } // namespace clangd
 } // namespace clang

diff  --git a/clang/lib/Sema/SemaCodeComplete.cpp 
b/clang/lib/Sema/SemaCodeComplete.cpp
index d77c9e43a9bd..c2785fd60fc2 100644
--- a/clang/lib/Sema/SemaCodeComplete.cpp
+++ b/clang/lib/Sema/SemaCodeComplete.cpp
@@ -3529,9 +3529,11 @@ CodeCompletionString 
*CodeCompletionResult::createCodeCompletionStringForDecl(
 Result.AddTypedTextChunk("");
 }
 unsigned Idx = 0;
+// The extra Idx < Sel.getNumArgs() check is needed due to legacy C-style
+// method parameters.
 for (ObjCMethodDecl::param_const_iterator P = Method->param_begin(),
   PEnd = Method->param_end();
- P != PEnd; (void)++P, ++Idx) {
+ P != PEnd && Idx < Sel.getNumArgs(); (void)++P, ++Idx) {
   if (Idx > 0) {
 std::string Keyword;
 if (Idx > StartParameter)



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] a71877e - [clang] Do not crash when CXXRecordDecl has a non-CXXRecordDecl base.

2021-01-14 Thread Adam Czachorowski via llvm-branch-commits

Author: Adam Czachorowski
Date: 2021-01-14T21:20:06+01:00
New Revision: a71877edfbb7094584f6d20d93f6091e7d374024

URL: 
https://github.com/llvm/llvm-project/commit/a71877edfbb7094584f6d20d93f6091e7d374024
DIFF: 
https://github.com/llvm/llvm-project/commit/a71877edfbb7094584f6d20d93f6091e7d374024.diff

LOG: [clang] Do not crash when CXXRecordDecl has a non-CXXRecordDecl base.

This can happen on some invalid code, like the included test case.

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

Added: 


Modified: 
clang/lib/Sema/SemaDeclCXX.cpp
clang/test/SemaTemplate/temp_class_spec.cpp

Removed: 




diff  --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 27679ac6f8d3..8bfaa46162bc 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -5520,8 +5520,9 @@ 
Sema::MarkBaseAndMemberDestructorsReferenced(SourceLocation Location,
 
   // Bases.
   for (const auto &Base : ClassDecl->bases()) {
-// Bases are always records in a well-formed non-dependent class.
 const RecordType *RT = Base.getType()->getAs();
+if (!RT)
+  continue;
 
 // Remember direct virtual bases.
 if (Base.isVirtual()) {

diff  --git a/clang/test/SemaTemplate/temp_class_spec.cpp 
b/clang/test/SemaTemplate/temp_class_spec.cpp
index 8a07fd7292c2..f92c52e9624e 100644
--- a/clang/test/SemaTemplate/temp_class_spec.cpp
+++ b/clang/test/SemaTemplate/temp_class_spec.cpp
@@ -361,3 +361,17 @@ namespace PR6181 {
   };
   
 }
+
+// Check that we do not crash on invalid code that leads to invalid base.
+namespace {
+template 
+class Foo {};
+
+template 
+class Bar;
+
+template 
+class Bar<0> : public Foo { // expected-error{{partial specialization of 
'Bar' does not use any of its template parameters}}
+  Bar() : Foo() {}
+};
+} // namespace



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang-tools-extra] aeaeb9e - [clangd] Make ExpandAutoType not available on template params.

2021-01-15 Thread Adam Czachorowski via llvm-branch-commits

Author: Adam Czachorowski
Date: 2021-01-15T14:19:05+01:00
New Revision: aeaeb9e6bdc90d9c4b839ac0e4edc6255021cced

URL: 
https://github.com/llvm/llvm-project/commit/aeaeb9e6bdc90d9c4b839ac0e4edc6255021cced
DIFF: 
https://github.com/llvm/llvm-project/commit/aeaeb9e6bdc90d9c4b839ac0e4edc6255021cced.diff

LOG: [clangd] Make ExpandAutoType not available on template params.

We cannot expand auto when used inside a template param (C++17 feature),
so do not offer it there.

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

Added: 


Modified: 
clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
clang-tools-extra/clangd/unittests/tweaks/ExpandAutoTypeTests.cpp

Removed: 




diff  --git a/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp 
b/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
index cb87cc764bc3..3776e1c3505d 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
@@ -82,6 +82,15 @@ bool isDeducedAsLambda(const SelectionTree::Node *Node, 
SourceLocation Loc) {
   return false;
 }
 
+// Returns true iff "auto" in Node is really part of the template parameter,
+// which we cannot expand.
+bool isTemplateParam(const SelectionTree::Node *Node) {
+  if (Node->Parent)
+if (Node->Parent->ASTNode.get())
+  return true;
+  return false;
+}
+
 bool ExpandAutoType::prepare(const Selection& Inputs) {
   CachedLocation = llvm::None;
   if (auto *Node = Inputs.ASTSelection.commonAncestor()) {
@@ -90,7 +99,8 @@ bool ExpandAutoType::prepare(const Selection& Inputs) {
 // Code in apply() does handle 'decltype(auto)' yet.
 if (!Result.getTypePtr()->isDecltypeAuto() &&
 !isStructuredBindingType(Node) &&
-!isDeducedAsLambda(Node, Result.getBeginLoc()))
+!isDeducedAsLambda(Node, Result.getBeginLoc()) &&
+!isTemplateParam(Node))
   CachedLocation = Result;
   }
 }

diff  --git a/clang-tools-extra/clangd/unittests/tweaks/ExpandAutoTypeTests.cpp 
b/clang-tools-extra/clangd/unittests/tweaks/ExpandAutoTypeTests.cpp
index 6c96ecc2332f..5554b68b31c7 100644
--- a/clang-tools-extra/clangd/unittests/tweaks/ExpandAutoTypeTests.cpp
+++ b/clang-tools-extra/clangd/unittests/tweaks/ExpandAutoTypeTests.cpp
@@ -80,6 +80,9 @@ TEST_F(ExpandAutoTypeTest, Test) {
   // unknown types in a template should not be replaced
   EXPECT_THAT(apply("template  void x() { ^auto y = T::z(); }"),
   StartsWith("fail: Could not deduce type for 'auto' type"));
+
+  ExtraArgs.push_back("-std=c++17");
+  EXPECT_UNAVAILABLE("template  class Y;");
 }
 
 } // namespace



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang-tools-extra] c77c3d1 - [clangd] Set correct CWD when using compile_flags.txt

2021-01-15 Thread Adam Czachorowski via llvm-branch-commits

Author: Adam Czachorowski
Date: 2021-01-15T14:26:24+01:00
New Revision: c77c3d1d18cdd58989f9d35bbf6c31f5fda0a125

URL: 
https://github.com/llvm/llvm-project/commit/c77c3d1d18cdd58989f9d35bbf6c31f5fda0a125
DIFF: 
https://github.com/llvm/llvm-project/commit/c77c3d1d18cdd58989f9d35bbf6c31f5fda0a125.diff

LOG: [clangd] Set correct CWD when using compile_flags.txt

This fixes a bug where clangd would attempt to set CWD to the
compile_flags.txt file itself.

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

Added: 


Modified: 
clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp

Removed: 




diff  --git a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp 
b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
index d983f76e227f..fde4e56ac72d 100644
--- a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
+++ b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp
@@ -271,7 +271,8 @@ parseJSON(PathRef Path, llvm::StringRef Data, std::string 
&Error) {
 }
 static std::unique_ptr
 parseFixed(PathRef Path, llvm::StringRef Data, std::string &Error) {
-  return tooling::FixedCompilationDatabase::loadFromBuffer(Path, Data, Error);
+  return tooling::FixedCompilationDatabase::loadFromBuffer(
+  llvm::sys::path::parent_path(Path), Data, Error);
 }
 
 bool DirectoryBasedGlobalCompilationDatabase::DirectoryCache::load(

diff  --git 
a/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp 
b/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp
index 63b1b731656a..fbb85684af5f 100644
--- a/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp
+++ b/clang-tools-extra/clangd/unittests/GlobalCompilationDatabaseTests.cpp
@@ -279,6 +279,17 @@ TEST(GlobalCompilationDatabaseTest, BuildDir) {
   << "x/build/compile_flags.json only applicable to x/";
 }
 
+TEST(GlobalCompilationDatabaseTest, CompileFlagsDirectory) {
+  MockFS FS;
+  FS.Files[testPath("x/compile_flags.txt")] = "-DFOO";
+  DirectoryBasedGlobalCompilationDatabase CDB(FS);
+  auto Commands = CDB.getCompileCommand(testPath("x/y.cpp"));
+  ASSERT_TRUE(Commands.hasValue());
+  EXPECT_THAT(Commands.getValue().CommandLine, Contains("-DFOO"));
+  // Make sure we pick the right working directory.
+  EXPECT_EQ(testPath("x"), Commands.getValue().Directory);
+}
+
 TEST(GlobalCompilationDatabaseTest, NonCanonicalFilenames) {
   OverlayCDB DB(nullptr);
   std::vector DiscoveredFiles;



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] 196cc96 - [clang] Allow LifetimeExtendedTemporary to have no access specifier

2021-01-18 Thread Adam Czachorowski via llvm-branch-commits

Author: Adam Czachorowski
Date: 2021-01-18T19:19:57+01:00
New Revision: 196cc96f9a643d1cb828f48ef15ec30d0de24df7

URL: 
https://github.com/llvm/llvm-project/commit/196cc96f9a643d1cb828f48ef15ec30d0de24df7
DIFF: 
https://github.com/llvm/llvm-project/commit/196cc96f9a643d1cb828f48ef15ec30d0de24df7.diff

LOG: [clang] Allow LifetimeExtendedTemporary to have no access specifier

The check only runs in debug mode during serialization, but
assert()-fail on:
  struct S { const int& x = 7; };
in C++ mode.

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

Added: 


Modified: 
clang/lib/AST/DeclBase.cpp
clang/test/PCH/cxx-reference.h

Removed: 




diff  --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp
index 0656efae5489..dc59f3dd1f15 100644
--- a/clang/lib/AST/DeclBase.cpp
+++ b/clang/lib/AST/DeclBase.cpp
@@ -971,21 +971,19 @@ bool Decl::AccessDeclContextSanity() const {
   // 5. it's invalid
   // 6. it's a C++0x static_assert.
   // 7. it's a block literal declaration
-  if (isa(this) ||
-  isa(this) ||
-  isa(this) ||
-  !getDeclContext() ||
-  !isa(getDeclContext()) ||
-  isInvalidDecl() ||
-  isa(this) ||
-  isa(this) ||
+  // 8. it's a temporary with lifetime extended due to being default value.
+  if (isa(this) || isa(this) ||
+  isa(this) || !getDeclContext() ||
+  !isa(getDeclContext()) || isInvalidDecl() ||
+  isa(this) || isa(this) ||
   // FIXME: a ParmVarDecl can have ClassTemplateSpecialization
   // as DeclContext (?).
   isa(this) ||
   // FIXME: a ClassTemplateSpecialization or CXXRecordDecl can have
   // AS_none as access specifier.
   isa(this) ||
-  isa(this))
+  isa(this) ||
+  isa(this))
 return true;
 
   assert(Access != AS_none &&

diff  --git a/clang/test/PCH/cxx-reference.h b/clang/test/PCH/cxx-reference.h
index b46a3671a325..a65d3feee072 100644
--- a/clang/test/PCH/cxx-reference.h
+++ b/clang/test/PCH/cxx-reference.h
@@ -11,3 +11,7 @@ LR &lrlr = c;
 LR &&rrlr = c;
 RR &lrrr = c;
 RR && = 'c';
+
+struct S {
+  const int &x = 1; // LifetimeExtendedTemporary inside struct
+};



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] a6f9077 - [clang] Check for nullptr when instantiating late attrs

2021-01-19 Thread Adam Czachorowski via llvm-branch-commits

Author: Adam Czachorowski
Date: 2021-01-19T13:43:15+01:00
New Revision: a6f9077b16da90204b296acd4f840769e83460ac

URL: 
https://github.com/llvm/llvm-project/commit/a6f9077b16da90204b296acd4f840769e83460ac
DIFF: 
https://github.com/llvm/llvm-project/commit/a6f9077b16da90204b296acd4f840769e83460ac.diff

LOG: [clang] Check for nullptr when instantiating late attrs

This was already done in SemaTemplateInstantiateDecl.cpp, but not in
SemaTemplateInstantiate.cpp.

Anecdotally I've seen some clangd crashes where coredumps point to this
being a problem, but I cannot reproduce this so far.

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

Added: 


Modified: 
clang/lib/Sema/SemaTemplateInstantiate.cpp

Removed: 




diff  --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp 
b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index cb74f08830c8..7679063ead71 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -2796,7 +2796,8 @@ Sema::InstantiateClass(SourceLocation 
PointOfInstantiation,
 
 Attr *NewAttr =
   instantiateTemplateAttribute(I->TmplAttr, Context, *this, TemplateArgs);
-I->NewDecl->addAttr(NewAttr);
+if (NewAttr)
+  I->NewDecl->addAttr(NewAttr);
 LocalInstantiationScope::deleteScopes(I->Scope,
   Instantiator.getStartingScope());
   }



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] d4af865 - [clangd] Fix type printing in the presence of qualifiers

2021-01-08 Thread Adam Czachorowski via llvm-branch-commits

Author: Adam Czachorowski
Date: 2021-01-08T17:00:39+01:00
New Revision: d4af86581e80ef0f7a6f4a4fff1c97260a726e71

URL: 
https://github.com/llvm/llvm-project/commit/d4af86581e80ef0f7a6f4a4fff1c97260a726e71
DIFF: 
https://github.com/llvm/llvm-project/commit/d4af86581e80ef0f7a6f4a4fff1c97260a726e71.diff

LOG: [clangd] Fix type printing in the presence of qualifiers

When printing QualType with qualifiers like "const", or pointing to an
elaborated type, we would print garbage like:
  std::const std::vector&
with the initial std:: being calculated correctly, but inserted in the
wrong place and the second std:: not removed (due to elaborated type).

This affected, among others, ExtractFunction and ExpandAuto tweaks.

This change introduces a new callback to PrintingPolicy, which allows us
to influence the printing of namespace qualifiers. In the future, the
same callback can be used to improve handling of "using namespace"
directives as well.

Fixes:
  https://github.com/clangd/clangd/issues/640 (ExtractFunction)
  https://github.com/clangd/clangd/issues/264 (ExpandAuto)
  First point of https://github.com/clangd/clangd/issues/524

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

Added: 


Modified: 
clang-tools-extra/clangd/AST.cpp
clang-tools-extra/clangd/unittests/tweaks/ExpandAutoTypeTests.cpp
clang-tools-extra/clangd/unittests/tweaks/ExtractFunctionTests.cpp
clang/include/clang/AST/PrettyPrinter.h
clang/lib/AST/TypePrinter.cpp

Removed: 




diff  --git a/clang-tools-extra/clangd/AST.cpp 
b/clang-tools-extra/clangd/AST.cpp
index 39ab48843b28..16298f3e0326 100644
--- a/clang-tools-extra/clangd/AST.cpp
+++ b/clang-tools-extra/clangd/AST.cpp
@@ -299,19 +299,27 @@ SymbolID getSymbolID(const llvm::StringRef MacroName, 
const MacroInfo *MI,
   return SymbolID(USR);
 }
 
-// FIXME: This should be handled while printing underlying decls instead.
 std::string printType(const QualType QT, const DeclContext &CurContext) {
   std::string Result;
   llvm::raw_string_ostream OS(Result);
-  auto Decls =
-  explicitReferenceTargets(DynTypedNode::create(QT), DeclRelation::Alias);
-  if (!Decls.empty())
-OS << getQualification(CurContext.getParentASTContext(), &CurContext,
-   Decls.front(),
-   
/*VisibleNamespaces=*/llvm::ArrayRef{});
   PrintingPolicy PP(CurContext.getParentASTContext().getPrintingPolicy());
-  PP.SuppressScope = true;
   PP.SuppressTagKeyword = true;
+  PP.SuppressUnwrittenScope = true;
+
+  class PrintCB : public PrintingCallbacks {
+  public:
+PrintCB(const DeclContext *CurContext) : CurContext(CurContext) {}
+virtual ~PrintCB() {}
+virtual bool isScopeVisible(const DeclContext *DC) const {
+  return DC->Encloses(CurContext);
+}
+
+  private:
+const DeclContext *CurContext;
+  };
+  PrintCB PCB(&CurContext);
+  PP.Callbacks = &PCB;
+
   QT.print(OS, PP);
   return OS.str();
 }

diff  --git a/clang-tools-extra/clangd/unittests/tweaks/ExpandAutoTypeTests.cpp 
b/clang-tools-extra/clangd/unittests/tweaks/ExpandAutoTypeTests.cpp
index ba783e551e84..6c96ecc2332f 100644
--- a/clang-tools-extra/clangd/unittests/tweaks/ExpandAutoTypeTests.cpp
+++ b/clang-tools-extra/clangd/unittests/tweaks/ExpandAutoTypeTests.cpp
@@ -65,6 +65,11 @@ TEST_F(ExpandAutoTypeTest, Test) {
   EXPECT_EQ(apply(R"cpp(au^to x = "test";)cpp"),
 R"cpp(const char * x = "test";)cpp");
 
+  EXPECT_EQ(apply("ns::Class * foo() { au^to c = foo(); }"),
+"ns::Class * foo() { ns::Class * c = foo(); }");
+  EXPECT_EQ(apply("void ns::Func() { au^to x = new ns::Class::Nested{}; }"),
+"void ns::Func() { Class::Nested * x = new ns::Class::Nested{}; 
}");
+
   EXPECT_UNAVAILABLE("dec^ltype(au^to) x = 10;");
   // expanding types in structured bindings is syntactically invalid.
   EXPECT_UNAVAILABLE("const ^auto &[x,y] = (int[]){1,2};");

diff  --git 
a/clang-tools-extra/clangd/unittests/tweaks/ExtractFunctionTests.cpp 
b/clang-tools-extra/clangd/unittests/tweaks/ExtractFunctionTests.cpp
index 2033b479896b..94cc4b0a0d84 100644
--- a/clang-tools-extra/clangd/unittests/tweaks/ExtractFunctionTests.cpp
+++ b/clang-tools-extra/clangd/unittests/tweaks/ExtractFunctionTests.cpp
@@ -97,6 +97,22 @@ void f(const int c) {
 })cpp";
   EXPECT_EQ(apply(ConstCheckInput), ConstCheckOutput);
 
+  // Check const qualifier with namespace
+  std::string ConstNamespaceCheckInput = R"cpp(
+namespace X { struct Y { int z; }; }
+int f(const X::Y &y) {
+  [[return y.z + y.z;]]
+})cpp";
+  std::string ConstNamespaceCheckOutput = R"cpp(
+namespace X { struct Y { int z; }; }
+int extracted(const X::Y &y) {
+return y.z + y.z;
+}
+int f(const X::Y &y) {
+  return extracted(y);
+})cpp";
+  EXPECT_EQ(apply(ConstNamespaceCheckInput), ConstNamespaceCheckOutput);
+
   // Don't extract when we need to make a function as a parameter.
   EXPECT_THAT(apply("void f() {

[llvm-branch-commits] [clang-tools-extra] 2e1bb79 - [clangd] Add missing "override" to fix the build.

2021-01-08 Thread Adam Czachorowski via llvm-branch-commits

Author: Adam Czachorowski
Date: 2021-01-08T17:24:47+01:00
New Revision: 2e1bb7940a4ddc847cebd25092d10f40866a7fad

URL: 
https://github.com/llvm/llvm-project/commit/2e1bb7940a4ddc847cebd25092d10f40866a7fad
DIFF: 
https://github.com/llvm/llvm-project/commit/2e1bb7940a4ddc847cebd25092d10f40866a7fad.diff

LOG: [clangd] Add missing "override" to fix the build.

Follow-up to d4af86581e80ef0f7a6f4a4fff1c97260a726e71

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

Added: 


Modified: 
clang-tools-extra/clangd/AST.cpp

Removed: 




diff  --git a/clang-tools-extra/clangd/AST.cpp 
b/clang-tools-extra/clangd/AST.cpp
index 16298f3e0326..8af4cbb19a3d 100644
--- a/clang-tools-extra/clangd/AST.cpp
+++ b/clang-tools-extra/clangd/AST.cpp
@@ -310,7 +310,7 @@ std::string printType(const QualType QT, const DeclContext 
&CurContext) {
   public:
 PrintCB(const DeclContext *CurContext) : CurContext(CurContext) {}
 virtual ~PrintCB() {}
-virtual bool isScopeVisible(const DeclContext *DC) const {
+virtual bool isScopeVisible(const DeclContext *DC) const override {
   return DC->Encloses(CurContext);
 }
 



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang-tools-extra] 0999408 - [clangd] Add error handling (elog) in code completion.

2020-12-28 Thread Adam Czachorowski via llvm-branch-commits

Author: Adam Czachorowski
Date: 2020-12-28T15:22:54+01:00
New Revision: 0999408aea79dd69f182cfcb618006f6cf2b6d4e

URL: 
https://github.com/llvm/llvm-project/commit/0999408aea79dd69f182cfcb618006f6cf2b6d4e
DIFF: 
https://github.com/llvm/llvm-project/commit/0999408aea79dd69f182cfcb618006f6cf2b6d4e.diff

LOG: [clangd] Add error handling (elog) in code completion.

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

Added: 


Modified: 
clang-tools-extra/clangd/CodeComplete.cpp

Removed: 




diff  --git a/clang-tools-extra/clangd/CodeComplete.cpp 
b/clang-tools-extra/clangd/CodeComplete.cpp
index cc6b5dbc9904..ebdbd56c286a 100644
--- a/clang-tools-extra/clangd/CodeComplete.cpp
+++ b/clang-tools-extra/clangd/CodeComplete.cpp
@@ -182,12 +182,18 @@ struct CompletionCandidate {
 // strings (literal or URI) mapping to the same file. We still want to
 // bundle those, so we must resolve the header to be included here.
 std::string HeaderForHash;
-if (Inserter)
-  if (auto Header = headerToInsertIfAllowed(Opts))
-if (auto HeaderFile = toHeaderFile(*Header, FileName))
+if (Inserter) {
+  if (auto Header = headerToInsertIfAllowed(Opts)) {
+if (auto HeaderFile = toHeaderFile(*Header, FileName)) {
   if (auto Spelled =
   Inserter->calculateIncludePath(*HeaderFile, FileName))
 HeaderForHash = *Spelled;
+} else {
+  vlog("Code completion header path manipulation failed {0}",
+   HeaderFile.takeError());
+}
+  }
+}
 
 llvm::SmallString<256> Scratch;
 if (IndexResult) {



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang-tools-extra] f697050 - [clangd] PopulateSwitch: disable on dependent enums.

2020-11-25 Thread Adam Czachorowski via llvm-branch-commits

Author: Adam Czachorowski
Date: 2020-11-25T14:12:29+01:00
New Revision: f6970503d291b7cae70fe583bed392387f93f9e4

URL: 
https://github.com/llvm/llvm-project/commit/f6970503d291b7cae70fe583bed392387f93f9e4
DIFF: 
https://github.com/llvm/llvm-project/commit/f6970503d291b7cae70fe583bed392387f93f9e4.diff

LOG: [clangd] PopulateSwitch: disable on dependent enums.

If the enum is a dependent type, we would crash somewhere in
getIntWidth(). -Wswitch diagnostic doesn't work on dependent enums
either.

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

Added: 


Modified: 
clang-tools-extra/clangd/refactor/tweaks/PopulateSwitch.cpp
clang-tools-extra/clangd/unittests/TweakTests.cpp

Removed: 




diff  --git a/clang-tools-extra/clangd/refactor/tweaks/PopulateSwitch.cpp 
b/clang-tools-extra/clangd/refactor/tweaks/PopulateSwitch.cpp
index 852888f6a043..bae80cdecf59 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/PopulateSwitch.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/PopulateSwitch.cpp
@@ -126,7 +126,7 @@ bool PopulateSwitch::prepare(const Selection &Sel) {
 return false;
 
   EnumD = EnumT->getDecl();
-  if (!EnumD)
+  if (!EnumD || EnumD->isDependentType())
 return false;
 
   // We trigger if there are any values in the enum that aren't covered by the

diff  --git a/clang-tools-extra/clangd/unittests/TweakTests.cpp 
b/clang-tools-extra/clangd/unittests/TweakTests.cpp
index fd815d2c4c27..4a2360dda739 100644
--- a/clang-tools-extra/clangd/unittests/TweakTests.cpp
+++ b/clang-tools-extra/clangd/unittests/TweakTests.cpp
@@ -3084,6 +3084,12 @@ TEST_F(PopulateSwitchTest, Test) {
   R""(enum Enum {A,B,b=B}; ^switch (A) {case A:case B:break;})"",
   "unavailable",
   },
+  {
+  // Enum is dependent type
+  File,
+  R""(template void f() {enum Enum {A}; ^switch (A) {}})"",
+  "unavailable",
+  },
   };
 
   for (const auto &Case : Cases) {



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang-tools-extra] 9d87739 - [clangd] AddUsing: do not crash on non-namespace using decls.

2020-11-26 Thread Adam Czachorowski via llvm-branch-commits

Author: Adam Czachorowski
Date: 2020-11-26T20:07:56+01:00
New Revision: 9d87739f664b5b454ff78a3016ab05a1987f0d7c

URL: 
https://github.com/llvm/llvm-project/commit/9d87739f664b5b454ff78a3016ab05a1987f0d7c
DIFF: 
https://github.com/llvm/llvm-project/commit/9d87739f664b5b454ff78a3016ab05a1987f0d7c.diff

LOG: [clangd] AddUsing: do not crash on non-namespace using decls.

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

Added: 


Modified: 
clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
clang-tools-extra/clangd/unittests/TweakTests.cpp

Removed: 




diff  --git a/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp 
b/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
index 4b3fbc02c411..b00c2716005c 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
@@ -152,12 +152,14 @@ findInsertionPoint(const Tweak::Selection &Inputs,
 if (SM.isBeforeInTranslationUnit(Inputs.Cursor, U->getUsingLoc()))
   // "Usings" is sorted, so we're done.
   break;
-if (U->getQualifier()->getAsNamespace()->getCanonicalDecl() ==
-QualifierToRemove.getNestedNameSpecifier()
-->getAsNamespace()
-->getCanonicalDecl() &&
-U->getName() == Name) {
-  return InsertionPointData();
+if (const auto *Namespace = U->getQualifier()->getAsNamespace()) {
+  if (Namespace->getCanonicalDecl() ==
+  QualifierToRemove.getNestedNameSpecifier()
+  ->getAsNamespace()
+  ->getCanonicalDecl() &&
+  U->getName() == Name) {
+return InsertionPointData();
+  }
 }
 
 // Insertion point will be before last UsingDecl that affects cursor

diff  --git a/clang-tools-extra/clangd/unittests/TweakTests.cpp 
b/clang-tools-extra/clangd/unittests/TweakTests.cpp
index 4a2360dda739..eefc50d754e2 100644
--- a/clang-tools-extra/clangd/unittests/TweakTests.cpp
+++ b/clang-tools-extra/clangd/unittests/TweakTests.cpp
@@ -2884,6 +2884,17 @@ using xx::yy;
 void fun() {
   yy();
 }
+)cpp"},
+// Existing using with non-namespace part.
+{R"cpp(
+#include "test.hpp"
+using one::two::ee::ee_one;
+one::t^wo::cc c;
+)cpp",
+ R"cpp(
+#include "test.hpp"
+using one::two::cc;using one::two::ee::ee_one;
+cc c;
 )cpp"}};
   llvm::StringMap EditedFiles;
   for (const auto &Case : Cases) {
@@ -2892,7 +2903,7 @@ void fun() {
 namespace one {
 void oo() {}
 namespace two {
-enum ee {};
+enum ee {ee_one};
 void ff() {}
 class cc {
 public:



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang-tools-extra] 517828a - [clangd] Bundle code completion items when the include paths differ, but resolve to the same file.

2020-12-03 Thread Adam Czachorowski via llvm-branch-commits

Author: Adam Czachorowski
Date: 2020-12-03T16:33:15+01:00
New Revision: 517828a31b0d1b7cfd1fd261046746bd8778420a

URL: 
https://github.com/llvm/llvm-project/commit/517828a31b0d1b7cfd1fd261046746bd8778420a
DIFF: 
https://github.com/llvm/llvm-project/commit/517828a31b0d1b7cfd1fd261046746bd8778420a.diff

LOG: [clangd] Bundle code completion items when the include paths differ, but 
resolve to the same file.

This can happen when, for example, merging results from an external
index that generates IncludeHeaders with full URI rather than just
literal include.

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

Added: 


Modified: 
clang-tools-extra/clangd/CodeComplete.cpp
clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp

Removed: 




diff  --git a/clang-tools-extra/clangd/CodeComplete.cpp 
b/clang-tools-extra/clangd/CodeComplete.cpp
index 1d85439f53af..cc6b5dbc9904 100644
--- a/clang-tools-extra/clangd/CodeComplete.cpp
+++ b/clang-tools-extra/clangd/CodeComplete.cpp
@@ -173,9 +173,22 @@ struct CompletionCandidate {
 
   // Returns a token identifying the overload set this is part of.
   // 0 indicates it's not part of any overload set.
-  size_t overloadSet(const CodeCompleteOptions &Opts) const {
+  size_t overloadSet(const CodeCompleteOptions &Opts, llvm::StringRef FileName,
+ IncludeInserter *Inserter) const {
 if (!Opts.BundleOverloads.getValueOr(false))
   return 0;
+
+// Depending on the index implementation, we can see 
diff erent header
+// strings (literal or URI) mapping to the same file. We still want to
+// bundle those, so we must resolve the header to be included here.
+std::string HeaderForHash;
+if (Inserter)
+  if (auto Header = headerToInsertIfAllowed(Opts))
+if (auto HeaderFile = toHeaderFile(*Header, FileName))
+  if (auto Spelled =
+  Inserter->calculateIncludePath(*HeaderFile, FileName))
+HeaderForHash = *Spelled;
+
 llvm::SmallString<256> Scratch;
 if (IndexResult) {
   switch (IndexResult->SymInfo.Kind) {
@@ -192,7 +205,7 @@ struct CompletionCandidate {
 // This could break #include insertion.
 return llvm::hash_combine(
 (IndexResult->Scope + IndexResult->Name).toStringRef(Scratch),
-headerToInsertIfAllowed(Opts).getValueOr(""));
+HeaderForHash);
   default:
 return 0;
   }
@@ -206,8 +219,7 @@ struct CompletionCandidate {
 llvm::raw_svector_ostream OS(Scratch);
 D->printQualifiedName(OS);
   }
-  return llvm::hash_combine(Scratch,
-headerToInsertIfAllowed(Opts).getValueOr(""));
+  return llvm::hash_combine(Scratch, HeaderForHash);
 }
 assert(IdentifierResult);
 return 0;
@@ -1570,7 +1582,8 @@ class CodeCompleteFlow {
 assert(IdentifierResult);
 C.Name = IdentifierResult->Name;
   }
-  if (auto OverloadSet = C.overloadSet(Opts)) {
+  if (auto OverloadSet = C.overloadSet(
+  Opts, FileName, Inserter ? Inserter.getPointer() : nullptr)) {
 auto Ret = BundleLookup.try_emplace(OverloadSet, Bundles.size());
 if (Ret.second)
   Bundles.emplace_back();

diff  --git a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp 
b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
index a7e1c6c48143..a19c6a83e954 100644
--- a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
+++ b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
@@ -1628,6 +1628,29 @@ TEST(CompletionTest, OverloadBundling) {
   EXPECT_EQ(A.SnippetSuffix, "($0)");
 }
 
+TEST(CompletionTest, OverloadBundlingSameFileDifferentURI) {
+  clangd::CodeCompleteOptions Opts;
+  Opts.BundleOverloads = true;
+
+  Symbol SymX = sym("ns::X", index::SymbolKind::Function, "@F@\\0#");
+  Symbol SymY = sym("ns::X", index::SymbolKind::Function, "@F@\\0#I#");
+  std::string BarHeader = testPath("bar.h");
+  auto BarURI = URI::create(BarHeader).toString();
+  SymX.CanonicalDeclaration.FileURI = BarURI.c_str();
+  SymY.CanonicalDeclaration.FileURI = BarURI.c_str();
+  // The include header is 
diff erent, but really it's the same file.
+  SymX.IncludeHeaders.emplace_back("\"bar.h\"", 1);
+  SymY.IncludeHeaders.emplace_back(BarURI.c_str(), 1);
+
+  auto Results = completions("void f() { ::ns::^ }", {SymX, SymY}, Opts);
+  // Expect both results are bundled, despite the 
diff erent-but-same
+  // IncludeHeader.
+  ASSERT_EQ(1u, Results.Completions.size());
+  const auto &R = Results.Completions.front();
+  EXPECT_EQ("X", R.Name);
+  EXPECT_EQ(2u, R.BundleSize);
+}
+
 TEST(CompletionTest, DocumentationFromChangedFileCrash) {
   MockFS FS;
   auto FooH = testPath("foo.h");



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi

[llvm-branch-commits] [clang-tools-extra] c282b7d - [clangd] AddUsing: Fix a crash on ElaboratedTypes without NestedNameSpecfiiers.

2020-12-03 Thread Adam Czachorowski via llvm-branch-commits

Author: Adam Czachorowski
Date: 2020-12-03T20:25:38+01:00
New Revision: c282b7de5a5de8151a19228702867e2299f1d3fe

URL: 
https://github.com/llvm/llvm-project/commit/c282b7de5a5de8151a19228702867e2299f1d3fe
DIFF: 
https://github.com/llvm/llvm-project/commit/c282b7de5a5de8151a19228702867e2299f1d3fe.diff

LOG: [clangd] AddUsing: Fix a crash on ElaboratedTypes without 
NestedNameSpecfiiers.

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

Added: 


Modified: 
clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
clang-tools-extra/clangd/unittests/TweakTests.cpp

Removed: 




diff  --git a/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp 
b/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
index b00c2716005c7..d6a57efeeef13 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp
@@ -274,6 +274,8 @@ bool AddUsing::prepare(const Selection &Inputs) {
   } else if (auto *T = Node->ASTNode.get()) {
 if (auto E = T->getAs()) {
   QualifierToRemove = E.getQualifierLoc();
+  if (!QualifierToRemove)
+return false;
 
   auto SpelledTokens =
   TB.spelledForExpanded(TB.expandedTokens(E.getSourceRange()));

diff  --git a/clang-tools-extra/clangd/unittests/TweakTests.cpp 
b/clang-tools-extra/clangd/unittests/TweakTests.cpp
index eefc50d754e2a..07f061b3f2e39 100644
--- a/clang-tools-extra/clangd/unittests/TweakTests.cpp
+++ b/clang-tools-extra/clangd/unittests/TweakTests.cpp
@@ -2523,6 +2523,9 @@ class cc {
   // Do not offer code action on typo-corrections.
   EXPECT_UNAVAILABLE(Header + "/*error-ok*/c^c C;");
 
+  // NestedNameSpecifier, but no namespace.
+  EXPECT_UNAVAILABLE(Header + "class Foo {}; class F^oo foo;");
+
   // Check that we do not trigger in header files.
   FileName = "test.h";
   ExtraArgs.push_back("-xc++-header"); // .h file is treated a C by default.



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang-tools-extra] f6b205d - [clangd] ExtractFunction: disable on regions that sometimes, but not always return.

2020-12-08 Thread Adam Czachorowski via llvm-branch-commits

Author: Adam Czachorowski
Date: 2020-12-08T15:55:32+01:00
New Revision: f6b205dae16392382324fbca676ef6afe3920642

URL: 
https://github.com/llvm/llvm-project/commit/f6b205dae16392382324fbca676ef6afe3920642
DIFF: 
https://github.com/llvm/llvm-project/commit/f6b205dae16392382324fbca676ef6afe3920642.diff

LOG: [clangd] ExtractFunction: disable on regions that sometimes, but not 
always return.

apply() will fail in those cases, so it's better to detect it in
prepare() already and hide code action from the user.

This was especially annoying on code bases that use a lot of
RETURN_IF_ERROR-like macros.

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

Added: 


Modified: 
clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp
clang-tools-extra/clangd/unittests/TweakTests.cpp

Removed: 




diff  --git a/clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp 
b/clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp
index 18fe7bf39160..8eec42876d6b 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/ExtractFunction.cpp
@@ -708,6 +708,27 @@ tooling::Replacement createFunctionDefinition(const 
NewFunction &ExtractedFunc,
   return tooling::Replacement(SM, ExtractedFunc.InsertionPoint, 0, 
FunctionDef);
 }
 
+// Returns true if ExtZone contains any ReturnStmts.
+bool hasReturnStmt(const ExtractionZone &ExtZone) {
+  class ReturnStmtVisitor
+  : public clang::RecursiveASTVisitor {
+  public:
+bool VisitReturnStmt(ReturnStmt *Return) {
+  Found = true;
+  return false; // We found the answer, abort the scan.
+}
+bool Found = false;
+  };
+
+  ReturnStmtVisitor V;
+  for (const Stmt *RootStmt : ExtZone.RootStmts) {
+V.TraverseStmt(const_cast(RootStmt));
+if (V.Found)
+  break;
+  }
+  return V.Found;
+}
+
 bool ExtractFunction::prepare(const Selection &Inputs) {
   const LangOptions &LangOpts = Inputs.AST->getLangOpts();
   if (!LangOpts.CPlusPlus)
@@ -715,8 +736,12 @@ bool ExtractFunction::prepare(const Selection &Inputs) {
   const Node *CommonAnc = Inputs.ASTSelection.commonAncestor();
   const SourceManager &SM = Inputs.AST->getSourceManager();
   auto MaybeExtZone = findExtractionZone(CommonAnc, SM, LangOpts);
+  if (!MaybeExtZone ||
+  (hasReturnStmt(*MaybeExtZone) && !alwaysReturns(*MaybeExtZone)))
+return false;
+
   // FIXME: Get rid of this check once we support hoisting.
-  if (!MaybeExtZone || MaybeExtZone->requiresHoisting(SM))
+  if (MaybeExtZone->requiresHoisting(SM))
 return false;
 
   ExtZone = std::move(*MaybeExtZone);

diff  --git a/clang-tools-extra/clangd/unittests/TweakTests.cpp 
b/clang-tools-extra/clangd/unittests/TweakTests.cpp
index 07f061b3f2e3..85edd92d27da 100644
--- a/clang-tools-extra/clangd/unittests/TweakTests.cpp
+++ b/clang-tools-extra/clangd/unittests/TweakTests.cpp
@@ -607,7 +607,11 @@ TEST_F(ExtractFunctionTest, FunctionTest) {
   // Extract certain return
   EXPECT_THAT(apply(" if(true) [[{ return; }]] "), HasSubstr("extracted"));
   // Don't extract uncertain return
-  EXPECT_THAT(apply(" if(true) [[if (false) return;]] "), StartsWith("fail"));
+  EXPECT_THAT(apply(" if(true) [[if (false) return;]] "),
+  StartsWith("unavailable"));
+  EXPECT_THAT(
+  apply("#define RETURN_IF_ERROR(x) if (x) return\nRETU^RN_IF_ERROR(4);"),
+  StartsWith("unavailable"));
 
   FileName = "a.c";
   EXPECT_THAT(apply(" for([[int i = 0;]];);"), HasSubstr("unavailable"));



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang-tools-extra] 3c5bed7 - [clangd] ExpandAutoType: Do not offer code action on lambdas.

2020-12-08 Thread Adam Czachorowski via llvm-branch-commits

Author: Adam Czachorowski
Date: 2020-12-08T20:03:16+01:00
New Revision: 3c5bed734f9e02bd3bc4fbd1e0acc53506823ebf

URL: 
https://github.com/llvm/llvm-project/commit/3c5bed734f9e02bd3bc4fbd1e0acc53506823ebf
DIFF: 
https://github.com/llvm/llvm-project/commit/3c5bed734f9e02bd3bc4fbd1e0acc53506823ebf.diff

LOG: [clangd] ExpandAutoType: Do not offer code action on lambdas.

We can't expand lambda types anyway. Now we simply not offer the code
action instead of showing it and then returning an error in apply().

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

Added: 


Modified: 
clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
clang-tools-extra/clangd/test/check-fail.test
clang-tools-extra/clangd/unittests/TweakTests.cpp

Removed: 




diff  --git a/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp 
b/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
index 61f68a688252..6d38eb1de82a 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
@@ -63,6 +63,25 @@ bool isStructuredBindingType(const SelectionTree::Node *N) {
   return N && N->ASTNode.get();
 }
 
+// Returns true iff Node is a lambda, and thus should not be expanded. Loc is
+// the location of the auto type.
+bool isDeducedAsLambda(const SelectionTree::Node *Node, SourceLocation Loc) {
+  // getDeducedType() does a traversal, which we want to avoid in prepare().
+  // But at least check this isn't auto x = []{...};, which can't ever be
+  // expanded.
+  // (It would be nice if we had an efficient getDeducedType(), instead).
+  for (const auto *It = Node; It; It = It->Parent) {
+if (const auto *DD = It->ASTNode.get()) {
+  if (DD->getTypeSourceInfo() &&
+  DD->getTypeSourceInfo()->getTypeLoc().getBeginLoc() == Loc) {
+if (auto *RD = DD->getType()->getAsRecordDecl())
+  return RD->isLambda();
+  }
+}
+  }
+  return false;
+}
+
 bool ExpandAutoType::prepare(const Selection& Inputs) {
   CachedLocation = llvm::None;
   if (auto *Node = Inputs.ASTSelection.commonAncestor()) {
@@ -70,11 +89,13 @@ bool ExpandAutoType::prepare(const Selection& Inputs) {
   if (const AutoTypeLoc Result = TypeNode->getAs()) {
 // Code in apply() does handle 'decltype(auto)' yet.
 if (!Result.getTypePtr()->isDecltypeAuto() &&
-!isStructuredBindingType(Node))
+!isStructuredBindingType(Node) &&
+!isDeducedAsLambda(Node, Result.getBeginLoc()))
   CachedLocation = Result;
   }
 }
   }
+
   return (bool) CachedLocation;
 }
 

diff  --git a/clang-tools-extra/clangd/test/check-fail.test 
b/clang-tools-extra/clangd/test/check-fail.test
index 0ee777f02cc5..dd50b59b2c79 100644
--- a/clang-tools-extra/clangd/test/check-fail.test
+++ b/clang-tools-extra/clangd/test/check-fail.test
@@ -11,4 +11,5 @@
 // CHECK: All checks completed, 2 errors
 
 #include "missing.h"
-auto x = []{};
+void fun();
+auto x = fun;

diff  --git a/clang-tools-extra/clangd/unittests/TweakTests.cpp 
b/clang-tools-extra/clangd/unittests/TweakTests.cpp
index 85edd92d27da..0dcf8feb786f 100644
--- a/clang-tools-extra/clangd/unittests/TweakTests.cpp
+++ b/clang-tools-extra/clangd/unittests/TweakTests.cpp
@@ -559,8 +559,7 @@ TEST_F(ExpandAutoTypeTest, Test) {
   EXPECT_THAT(apply("au^to x = &ns::Func;"),
   StartsWith("fail: Could not expand type of function pointer"));
   // lambda types are not replaced
-  EXPECT_THAT(apply("au^to x = []{};"),
-  StartsWith("fail: Could not expand type of lambda expression"));
+  EXPECT_UNAVAILABLE("au^to x = []{};");
   // inline namespaces
   EXPECT_EQ(apply("au^to x = inl_ns::Visible();"),
 "Visible x = inl_ns::Visible();");



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits