[clang-tools-extra] r363365 - Test commit

2019-06-15 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Fri Jun 14 02:40:42 2019
New Revision: 363365

URL: http://llvm.org/viewvc/llvm-project?rev=363365&view=rev
Log:
Test commit

Modified:
clang-tools-extra/trunk/clangd/refactor/tweaks/RawStringLiteral.cpp

Modified: clang-tools-extra/trunk/clangd/refactor/tweaks/RawStringLiteral.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/refactor/tweaks/RawStringLiteral.cpp?rev=363365&r1=363364&r2=363365&view=diff
==
--- clang-tools-extra/trunk/clangd/refactor/tweaks/RawStringLiteral.cpp 
(original)
+++ clang-tools-extra/trunk/clangd/refactor/tweaks/RawStringLiteral.cpp Fri Jun 
14 02:40:42 2019
@@ -99,4 +99,3 @@ std::string RawStringLiteral::title() co
 } // namespace
 } // namespace clangd
 } // namespace clang
-


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


[clang-tools-extra] r365894 - [clangd] Fixed toHalfOpenFileRange

2019-07-12 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Fri Jul 12 04:42:31 2019
New Revision: 365894

URL: http://llvm.org/viewvc/llvm-project?rev=365894&view=rev
Log:
[clangd] Fixed toHalfOpenFileRange

Summary:
- Fixed toHalfOpenFileRange to work for macros as well as template
instantiations
- Added unit tests

Breaking test case for older version of toHalfOpenFileRange:
\# define FOO(X) X++
int a = 1;
int b = FOO(a);
toHalfOpenFileRange for the sourceRange of VarDecl for b returned the
wrong Range.

Reviewers: sammccall, kadircet

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

Tags: #clang

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

Modified:
clang-tools-extra/trunk/clangd/SourceCode.cpp
clang-tools-extra/trunk/clangd/SourceCode.h
clang-tools-extra/trunk/clangd/unittests/SourceCodeTests.cpp

Modified: clang-tools-extra/trunk/clangd/SourceCode.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SourceCode.cpp?rev=365894&r1=365893&r2=365894&view=diff
==
--- clang-tools-extra/trunk/clangd/SourceCode.cpp (original)
+++ clang-tools-extra/trunk/clangd/SourceCode.cpp Fri Jul 12 04:42:31 2019
@@ -12,6 +12,8 @@
 #include "Logger.h"
 #include "Protocol.h"
 #include "clang/AST/ASTContext.h"
+#include "clang/Basic/LangOptions.h"
+#include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Basic/TokenKinds.h"
 #include "clang/Format/Format.h"
@@ -244,20 +246,106 @@ bool halfOpenRangeTouches(const SourceMa
   return L == R.getEnd() || halfOpenRangeContains(Mgr, R, L);
 }
 
-llvm::Optional toHalfOpenFileRange(const SourceManager &Mgr,
+static unsigned getTokenLengthAtLoc(SourceLocation Loc, const SourceManager 
&SM,
+const LangOptions &LangOpts) {
+  Token TheTok;
+  if (Lexer::getRawToken(Loc, TheTok, SM, LangOpts))
+return 0;
+  // FIXME: Here we check whether the token at the location is a greatergreater
+  // (>>) token and consider it as a single greater (>). This is to get it
+  // working for templates but it isn't correct for the right shift operator. 
We
+  // can avoid this by using half open char ranges in getFileRange() but 
getting
+  // token ending is not well supported in macroIDs.
+  if (TheTok.is(tok::greatergreater))
+return 1;
+  return TheTok.getLength();
+}
+
+// Returns location of the last character of the token at a given loc
+static SourceLocation getLocForTokenEnd(SourceLocation BeginLoc,
+const SourceManager &SM,
+const LangOptions &LangOpts) {
+  unsigned Len = getTokenLengthAtLoc(BeginLoc, SM, LangOpts);
+  return BeginLoc.getLocWithOffset(Len ? Len - 1 : 0);
+}
+
+// Returns location of the starting of the token at a given EndLoc
+static SourceLocation getLocForTokenBegin(SourceLocation EndLoc,
+  const SourceManager &SM,
+  const LangOptions &LangOpts) {
+  return EndLoc.getLocWithOffset(
+  -(signed)getTokenLengthAtLoc(EndLoc, SM, LangOpts));
+}
+
+// Converts a char source range to a token range.
+static SourceRange toTokenRange(CharSourceRange Range, const SourceManager &SM,
+const LangOptions &LangOpts) {
+  if (!Range.isTokenRange())
+Range.setEnd(getLocForTokenBegin(Range.getEnd(), SM, LangOpts));
+  return Range.getAsRange();
+}
+// Returns the union of two token ranges.
+// To find the maximum of the Ends of the ranges, we compare the location of 
the
+// last character of the token.
+static SourceRange unionTokenRange(SourceRange R1, SourceRange R2,
+   const SourceManager &SM,
+   const LangOptions &LangOpts) {
+  SourceLocation E1 = getLocForTokenEnd(R1.getEnd(), SM, LangOpts);
+  SourceLocation E2 = getLocForTokenEnd(R2.getEnd(), SM, LangOpts);
+  return SourceRange(std::min(R1.getBegin(), R2.getBegin()),
+ E1 < E2 ? R2.getEnd() : R1.getEnd());
+}
+
+// Returns the tokenFileRange for a given Location as a Token Range
+// This is quite similar to getFileLoc in SourceManager as both use
+// getImmediateExpansionRange and getImmediateSpellingLoc (for macro IDs).
+// However:
+// - We want to maintain the full range information as we move from one file to
+//   the next. getFileLoc only uses the BeginLoc of getImmediateExpansionRange.
+// - We want to split '>>' tokens as the lexer parses the '>>' in template
+//   instantiations as a '>>' instead of a '>'.
+// There is also getExpansionRange but it simply calls
+// getImmediateExpansionRange on the begin and ends separately which is wrong.
+static SourceRange getTokenFileRange(SourceLocation Loc,
+ const SourceManager &SM,
+ const LangOptions &LangOpts) {
+  SourceRange FileRange = Lo

[clang-tools-extra] r366451 - [Clangd] Changed ExtractVariable to only work on non empty selections

2019-07-18 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Thu Jul 18 08:38:03 2019
New Revision: 366451

URL: http://llvm.org/viewvc/llvm-project?rev=366451&view=rev
Log:
[Clangd] Changed ExtractVariable to only work on non empty selections

Summary:
- For now, we don't trigger in any case if it's an empty selection
- Fixed unittests

Reviewers: kadircet, sammccall

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

Tags: #clang

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

Modified:
clang-tools-extra/trunk/clangd/refactor/Tweak.cpp
clang-tools-extra/trunk/clangd/refactor/Tweak.h
clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp
clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp

Modified: clang-tools-extra/trunk/clangd/refactor/Tweak.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/refactor/Tweak.cpp?rev=366451&r1=366450&r2=366451&view=diff
==
--- clang-tools-extra/trunk/clangd/refactor/Tweak.cpp (original)
+++ clang-tools-extra/trunk/clangd/refactor/Tweak.cpp Thu Jul 18 08:38:03 2019
@@ -40,7 +40,8 @@ void validateRegistry() {
 
 Tweak::Selection::Selection(ParsedAST &AST, unsigned RangeBegin,
 unsigned RangeEnd)
-: AST(AST), ASTSelection(AST.getASTContext(), RangeBegin, RangeEnd) {
+: AST(AST), SelectionBegin(RangeBegin), SelectionEnd(RangeEnd),
+  ASTSelection(AST.getASTContext(), RangeBegin, RangeEnd) {
   auto &SM = AST.getSourceManager();
   Code = SM.getBufferData(SM.getMainFileID());
   Cursor = SM.getComposedLoc(SM.getMainFileID(), RangeBegin);

Modified: clang-tools-extra/trunk/clangd/refactor/Tweak.h
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/refactor/Tweak.h?rev=366451&r1=366450&r2=366451&view=diff
==
--- clang-tools-extra/trunk/clangd/refactor/Tweak.h (original)
+++ clang-tools-extra/trunk/clangd/refactor/Tweak.h Thu Jul 18 08:38:03 2019
@@ -46,7 +46,12 @@ public:
 /// Parsed AST of the active file.
 ParsedAST &AST;
 /// A location of the cursor in the editor.
+// FIXME: Cursor is redundant and should be removed
 SourceLocation Cursor;
+/// The begin offset of the selection
+unsigned SelectionBegin;
+/// The end offset of the selection
+unsigned SelectionEnd;
 /// The AST nodes that were selected.
 SelectionTree ASTSelection;
 // FIXME: provide a way to get sources and ASTs for other files.

Modified: clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp?rev=366451&r1=366450&r2=366451&view=diff
==
--- clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp 
(original)
+++ clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp Thu Jul 
18 08:38:03 2019
@@ -219,7 +219,8 @@ bool ExtractVariable::prepare(const Sele
   const ASTContext &Ctx = Inputs.AST.getASTContext();
   const SourceManager &SM = Inputs.AST.getSourceManager();
   const SelectionTree::Node *N = Inputs.ASTSelection.commonAncestor();
-  if (!N)
+  // we don't trigger on empty selections for now
+  if (!N || Inputs.SelectionBegin == Inputs.SelectionEnd)
 return false;
   Target = llvm::make_unique(N, SM, Ctx);
   return Target->isExtractable();

Modified: clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp?rev=366451&r1=366450&r2=366451&view=diff
==
--- clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp (original)
+++ clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp Thu Jul 18 08:38:03 
2019
@@ -296,35 +296,36 @@ TEST(TweakTest, ExtractVariable) {
   checkAvailable(ID, R"cpp(
 int xyz() {
   // return statement
-  return ^1;
+  return [[1]];
 }
 void f() {
-  int a = 5 + [[4 ^* ^xyz^()]];
+  int a = [[5 +]] [[4 * xyz]]();
   // multivariable initialization
   if(1)
-int x = ^1, y = ^a + 1, a = ^1, z = a + 1;
+int x = [[1]], y = [[a]] + 1, a = [[1]], z = a + 1;
   // if without else
-  if(^1) {}
+  if([[1]])
+a = [[1]];
   // if with else
-  if(a < ^3)
-if(a == ^4)
-  a = ^5;
+  if(a < [[3]])
+if(a == [[4]])
+  a = [[5]];
 else
-  a = ^6;
-  else if (a < ^4)
-a = ^4;
+  a = [[5]];
+  else if (a < [[4]])
+a = [[4]];
   else
-a = ^5;
+a = [[5]];
   // for loop 
-  for(a = ^1; a > ^3^+^4; a++)
-a = ^2;
+  for(a = [[1]]; a > 3]] + [[4; a++)
+a = [[2]];
   // while 
-  wh

[clang-tools-extra] r366452 - [Clangd] NFC: Fixed tweaks CMakeLists order to alphabetical

2019-07-18 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Thu Jul 18 08:42:09 2019
New Revision: 366452

URL: http://llvm.org/viewvc/llvm-project?rev=366452&view=rev
Log:
[Clangd] NFC: Fixed tweaks CMakeLists order to alphabetical

Modified:
clang-tools-extra/trunk/clangd/refactor/tweaks/CMakeLists.txt

Modified: clang-tools-extra/trunk/clangd/refactor/tweaks/CMakeLists.txt
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/refactor/tweaks/CMakeLists.txt?rev=366452&r1=366451&r2=366452&view=diff
==
--- clang-tools-extra/trunk/clangd/refactor/tweaks/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clangd/refactor/tweaks/CMakeLists.txt Thu Jul 18 
08:42:09 2019
@@ -14,11 +14,11 @@ set(LLVM_LINK_COMPONENTS
 add_clang_library(clangDaemonTweaks OBJECT
   AnnotateHighlightings.cpp
   DumpAST.cpp
+  ExpandAutoType.cpp
   ExpandMacro.cpp
+  ExtractVariable.cpp
   RawStringLiteral.cpp
   SwapIfBranches.cpp
-  ExtractVariable.cpp
-  ExpandAutoType.cpp
 
   LINK_LIBS
   clangAST


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


[clang-tools-extra] r366566 - [Clangd] Fixed SelectionTree bug for macros

2019-07-19 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Fri Jul 19 04:41:02 2019
New Revision: 366566

URL: http://llvm.org/viewvc/llvm-project?rev=366566&view=rev
Log:
[Clangd] Fixed SelectionTree bug for macros

Summary:
Fixed SelectionTree bug for macros
- Fixed SelectionTree claimRange for macros and template instantiations
- Fixed SelectionTree unit tests
- Changed a breaking test in TweakTests

Reviewers: sammccall, kadircet

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

Tags: #clang

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

Modified:
clang-tools-extra/trunk/clangd/Selection.cpp
clang-tools-extra/trunk/clangd/unittests/SelectionTests.cpp
clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp

Modified: clang-tools-extra/trunk/clangd/Selection.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/Selection.cpp?rev=366566&r1=366565&r2=366566&view=diff
==
--- clang-tools-extra/trunk/clangd/Selection.cpp (original)
+++ clang-tools-extra/trunk/clangd/Selection.cpp Fri Jul 19 04:41:02 2019
@@ -8,10 +8,13 @@
 
 #include "Selection.h"
 #include "ClangdUnit.h"
+#include "SourceCode.h"
 #include "clang/AST/ASTTypeTraits.h"
 #include "clang/AST/PrettyPrinter.h"
 #include "clang/AST/RecursiveASTVisitor.h"
 #include "clang/AST/TypeLoc.h"
+#include "clang/Basic/SourceManager.h"
+#include "clang/Basic/TokenKinds.h"
 #include "llvm/ADT/STLExtras.h"
 #include 
 
@@ -239,26 +242,23 @@ private:
   SelectionTree::Selection claimRange(SourceRange S) {
 if (!S.isValid())
   return SelectionTree::Unselected;
-// getTopMacroCallerLoc() allows selection of constructs in macro args. 
e.g:
+// toHalfOpenFileRange() allows selection of constructs in macro args. e.g:
 //   #define LOOP_FOREVER(Body) for(;;) { Body }
 //   void IncrementLots(int &x) {
 // LOOP_FOREVER( ++x; )
 //   }
 // Selecting "++x" or "x" will do the right thing.
-auto B = SM.getDecomposedLoc(SM.getTopMacroCallerLoc(S.getBegin()));
-auto E = SM.getDecomposedLoc(SM.getTopMacroCallerLoc(S.getEnd()));
+auto Range = toHalfOpenFileRange(SM, LangOpts, S);
+assert(Range && "We should be able to get the File Range");
+auto B = SM.getDecomposedLoc(Range->getBegin());
+auto E = SM.getDecomposedLoc(Range->getEnd());
 // Otherwise, nodes in macro expansions can't be selected.
 if (B.first != SelFile || E.first != SelFile)
   return SelectionTree::Unselected;
-// Cheap test: is there any overlap at all between the selection and range?
-// Note that E.second is the *start* of the last token, which is why we
-// compare against the "rounded-down" SelBegin.
-if (B.second >= SelEnd || E.second < SelBeginTokenStart)
+// Is there any overlap at all between the selection and range?
+if (B.second >= SelEnd || E.second < SelBegin)
   return SelectionTree::Unselected;
-
-// We may have hit something, need some more precise checks.
-// Adjust [B, E) to be a half-open character range.
-E.second += Lexer::MeasureTokenLength(S.getEnd(), SM, LangOpts);
+// We may have hit something.
 auto PreciseBounds = std::make_pair(B.second, E.second);
 // Trim range using the selection, drop it if empty.
 B.second = std::max(B.second, SelBegin);

Modified: clang-tools-extra/trunk/clangd/unittests/SelectionTests.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SelectionTests.cpp?rev=366566&r1=366565&r2=366566&view=diff
==
--- clang-tools-extra/trunk/clangd/unittests/SelectionTests.cpp (original)
+++ clang-tools-extra/trunk/clangd/unittests/SelectionTests.cpp Fri Jul 19 
04:41:02 2019
@@ -37,15 +37,15 @@ SelectionTree makeSelectionTree(const St
 Range nodeRange(const SelectionTree::Node *N, ParsedAST &AST) {
   if (!N)
 return Range{};
-  SourceManager &SM = AST.getSourceManager();
+  const SourceManager &SM = AST.getSourceManager();
+  const LangOptions &LangOpts = AST.getASTContext().getLangOpts();
   StringRef Buffer = SM.getBufferData(SM.getMainFileID());
-  SourceRange SR = N->ASTNode.getSourceRange();
-  SR.setBegin(SM.getFileLoc(SR.getBegin()));
-  SR.setEnd(SM.getFileLoc(SR.getEnd()));
-  CharSourceRange R =
-  Lexer::getAsCharRange(SR, SM, AST.getASTContext().getLangOpts());
-  return Range{offsetToPosition(Buffer, SM.getFileOffset(R.getBegin())),
-   offsetToPosition(Buffer, SM.getFileOffset(R.getEnd()))};
+  auto FileRange =
+  toHalfOpenFileRange(SM, LangOpts, N->ASTNode.getSourceRange());
+  assert(FileRange && "We should be able to get the File Range");
+  return Range{
+  offsetToPosition(Buffer, SM.getFileOffset(FileRange->getBegin())),
+  offsetToPosition(Buffer, SM.getFileOffset(FileRange->getEnd()))};
 }
 
 std::string nodeKind(const SelectionTree::Node *N) {
@@ -144,17 +144,17 @@ TEST(Select

[clang-tools-extra] r366568 - [Clangd] Fixed ExtractVariable test

2019-07-19 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Fri Jul 19 05:11:04 2019
New Revision: 366568

URL: http://llvm.org/viewvc/llvm-project?rev=366568&view=rev
Log:
[Clangd] Fixed ExtractVariable test

Modified:
clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp

Modified: clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp?rev=366568&r1=366567&r2=366568&view=diff
==
--- clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp (original)
+++ clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp Fri Jul 19 05:11:04 
2019
@@ -398,17 +398,23 @@ TEST(TweakTest, ExtractVariable) {
}
  })cpp"},*/
   // ensure InsertionPoint isn't inside a macro
-  // FIXME: SelectionTree needs to be fixed for macros
-  /*{R"cpp(#define LOOP(x) while (1) {a = x;}
+  {R"cpp(#define LOOP(x) while (1) {a = x;}
  void f(int a) {
if(1)
 LOOP(5 + [[3]])
  })cpp",
- R"cpp(#define LOOP(x) while (1) {a = x;}
+   /*FIXME: It should be extracted like this. SelectionTree needs to be
+* fixed for macros.
+ R"cpp(#define LOOP(x) while (1) {a = x;}
+   void f(int a) {
+ auto dummy = 3; if(1)
+  LOOP(5 + dummy)
+   })cpp"},*/
+   R"cpp(#define LOOP(x) while (1) {a = x;}
  void f(int a) {
-   auto dummy = 3; if(1)
-LOOP(5 + dummy)
- })cpp"},*/
+   auto dummy = LOOP(5 + 3); if(1)
+dummy
+ })cpp"},
   {R"cpp(#define LOOP(x) do {x;} while(1);
  void f(int a) {
if(1)
@@ -426,15 +432,15 @@ TEST(TweakTest, ExtractVariable) {
R"cpp(void f(int a) {
 auto dummy = 1; label: [ [gsl::suppress("type")] ] for 
(;;) a = dummy;
  })cpp"},
-  // FIXME: Doesn't work because bug in selection tree
-  /*{R"cpp(#define PLUS(x) x++
+  // macro testing
+  {R"cpp(#define PLUS(x) x++
  void f(int a) {
PLUS([[a]]);
  })cpp",
R"cpp(#define PLUS(x) x++
  void f(int a) {
auto dummy = a; PLUS(dummy);
- })cpp"},*/
+ })cpp"},
   // FIXME: Doesn't work correctly for \[\[clang::uninitialized\]\] int
   // b = [[1]]; since the attr is inside the DeclStmt and the bounds of
   // DeclStmt don't cover the attribute


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


[clang-tools-extra] r366869 - [Clangd] Fixed ExtractVariable for certain types of Exprs

2019-07-23 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Tue Jul 23 22:42:55 2019
New Revision: 366869

URL: http://llvm.org/viewvc/llvm-project?rev=366869&view=rev
Log:
[Clangd] Fixed ExtractVariable for certain types of Exprs

Summary:

- Modified ExtractVariable for extraction of MemberExpr, DeclRefExpr and 
Assignment Expr
- Removed extraction from label statements.
- Fixed unittests

Reviewers: sammccall, kadircet

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

Tags: #clang

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

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

Modified: clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp?rev=366869&r1=366868&r2=366869&view=diff
==
--- clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp 
(original)
+++ clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp Tue Jul 
23 22:42:55 2019
@@ -13,6 +13,7 @@
 #include "refactor/Tweak.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Expr.h"
+#include "clang/AST/ExprCXX.h"
 #include "clang/AST/OperationKinds.h"
 #include "clang/AST/RecursiveASTVisitor.h"
 #include "clang/AST/Stmt.h"
@@ -77,29 +78,15 @@ computeReferencedDecls(const clang::Expr
   return Visitor.ReferencedDecls;
 }
 
-// An expr is not extractable if it's null or an expression of type void
-// FIXME: Ignore assignment (a = 1) Expr since it is extracted as dummy = a =
-static bool isExtractableExpr(const clang::Expr *Expr) {
-  if (Expr) {
-const Type *ExprType = Expr->getType().getTypePtrOrNull();
-// FIXME: check if we need to cover any other types
-if (ExprType)
-  return !ExprType->isVoidType();
-  }
-  return false;
-}
-
 ExtractionContext::ExtractionContext(const SelectionTree::Node *Node,
  const SourceManager &SM,
  const ASTContext &Ctx)
 : ExprNode(Node), SM(SM), Ctx(Ctx) {
   Expr = Node->ASTNode.get();
-  if (isExtractableExpr(Expr)) {
-ReferencedDecls = computeReferencedDecls(Expr);
-InsertionPoint = computeInsertionPoint();
-if (InsertionPoint)
-  Extractable = true;
-  }
+  ReferencedDecls = computeReferencedDecls(Expr);
+  InsertionPoint = computeInsertionPoint();
+  if (InsertionPoint)
+Extractable = true;
 }
 
 // checks whether extracting before InsertionPoint will take a
@@ -121,9 +108,9 @@ bool ExtractionContext::exprIsValidOutsi
 // the current Stmt. We ALWAYS insert before a Stmt whose parent is a
 // CompoundStmt
 //
-
-// FIXME: Extraction from switch and case statements
+// FIXME: Extraction from label, switch and case statements
 // FIXME: Doens't work for FoldExpr
+// FIXME: Ensure extraction from loops doesn't change semantics.
 const clang::Stmt *ExtractionContext::computeInsertionPoint() const {
   // returns true if we can extract before InsertionPoint
   auto CanExtractOutside =
@@ -141,8 +128,7 @@ const clang::Stmt *ExtractionContext::co
   return isa(Stmt) || isa(Stmt) ||
  isa(Stmt) || isa(Stmt) ||
  isa(Stmt) || isa(Stmt) || isa(Stmt) ||
- isa(Stmt) || isa(Stmt) ||
- isa(Stmt);
+ isa(Stmt) || isa(Stmt);
 }
 if (InsertionPoint->ASTNode.get())
   return true;
@@ -209,6 +195,9 @@ public:
 return "Extract subexpression to variable";
   }
   Intent intent() const override { return Refactor; }
+  // Compute the extraction context for the Selection
+  bool computeExtractionContext(const SelectionTree::Node *N,
+const SourceManager &SM, const ASTContext 
&Ctx);
 
 private:
   // the expression to extract
@@ -216,14 +205,13 @@ private:
 };
 REGISTER_TWEAK(ExtractVariable)
 bool ExtractVariable::prepare(const Selection &Inputs) {
+  // we don't trigger on empty selections for now
+  if (Inputs.SelectionBegin == Inputs.SelectionEnd)
+return false;
   const ASTContext &Ctx = Inputs.AST.getASTContext();
   const SourceManager &SM = Inputs.AST.getSourceManager();
   const SelectionTree::Node *N = Inputs.ASTSelection.commonAncestor();
-  // we don't trigger on empty selections for now
-  if (!N || Inputs.SelectionBegin == Inputs.SelectionEnd)
-return false;
-  Target = llvm::make_unique(N, SM, Ctx);
-  return Target->isExtractable();
+  return computeExtractionContext(N, SM, Ctx);
 }
 
 Expected ExtractVariable::apply(const Selection &Inputs) {
@@ -239,6 +227,75 @@ Expected ExtractVariable:
   return Effect::applyEdit(Result);
 }
 
+// Find the CallExpr whose callee is an ancestor of the DeclRef
+const SelectionTree::Node *getCallExpr(const SelectionTree::Node *DeclRef) {
+  // we maintain a stack of all exprs encountered while traversing the
+  // selectiontree because the callee of th

[clang-tools-extra] r367113 - [Clangd] Disable ExtractVariable for all types of assignments

2019-07-26 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Fri Jul 26 07:08:27 2019
New Revision: 367113

URL: http://llvm.org/viewvc/llvm-project?rev=367113&view=rev
Log:
[Clangd] Disable ExtractVariable for all types of assignments

Reviewers: sammccall, kadircet

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

Tags: #clang

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

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

Modified: clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp?rev=367113&r1=367112&r2=367113&view=diff
==
--- clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp 
(original)
+++ clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp Fri Jul 
26 07:08:27 2019
@@ -272,7 +272,7 @@ bool ExtractVariable::computeExtractionC
   // Extracting Exprs like a = 1 gives dummy = a = 1 which isn't useful.
   if (const BinaryOperator *BinOpExpr =
   dyn_cast_or_null(SelectedExpr)) {
-if (BinOpExpr->getOpcode() == BinaryOperatorKind::BO_Assign)
+if (BinOpExpr->isAssignmentOp())
   return false;
   }
   // For function and member function DeclRefs, we look for a parent that is a

Modified: clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp?rev=367113&r1=367112&r2=367113&view=diff
==
--- clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp (original)
+++ clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp Fri Jul 26 07:08:27 
2019
@@ -371,6 +371,8 @@ TEST(TweakTest, ExtractVariable) {
   auto lamb = [&[[a]], &[[b]]](int r = [[1]]) {return 1;}
   // assigment
   [[a = 5]];
+  [[a >>= 5]];
+  [[a *= 5]];
   // Variable DeclRefExpr
   a = [[b]];
   // label statement


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


[clang-tools-extra] r367406 - [Clangd] NFC: Added FIXME in ExtractVariable tests

2019-07-31 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Wed Jul 31 03:08:29 2019
New Revision: 367406

URL: http://llvm.org/viewvc/llvm-project?rev=367406&view=rev
Log:
[Clangd] NFC: Added FIXME in ExtractVariable tests

Modified:
clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp

Modified: clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp?rev=367406&r1=367405&r2=367406&view=diff
==
--- clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp (original)
+++ clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp Wed Jul 31 03:08:29 
2019
@@ -419,6 +419,11 @@ TEST(TweakTest, ExtractVariable) {
  void f(int a) {
PLUS([[1+a]]);
  })cpp",
+  /*FIXME: It should be extracted like this.
+   R"cpp(#define PLUS(x) x++
+ void f(int a) {
+   auto dummy = 1+a; int y = PLUS(dummy);
+ })cpp"},*/
R"cpp(#define PLUS(x) x++
  void f(int a) {
auto dummy = PLUS(1+a); dummy;
@@ -429,9 +434,9 @@ TEST(TweakTest, ExtractVariable) {
if(1)
 LOOP(5 + [[3]])
  })cpp",
-   /*FIXME: It should be extracted like this. SelectionTree needs to be
+  /*FIXME: It should be extracted like this. SelectionTree needs to be
 * fixed for macros.
- R"cpp(#define LOOP(x) while (1) {a = x;}
+   R"cpp(#define LOOP(x) while (1) {a = x;}
void f(int a) {
  auto dummy = 3; if(1)
   LOOP(5 + dummy)


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


[clang-tools-extra] r368058 - Fixed toHalfOpenFileRange assertion fail

2019-08-06 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Tue Aug  6 10:01:12 2019
New Revision: 368058

URL: http://llvm.org/viewvc/llvm-project?rev=368058&view=rev
Log:
Fixed toHalfOpenFileRange assertion fail

Summary:
- Added new function that gets Expansion range with both ends in the same file.
- Fixes the crash at https://github.com/clangd/clangd/issues/113

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

Tags: #clang

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

Modified:
clang-tools-extra/trunk/clangd/SourceCode.cpp
clang-tools-extra/trunk/clangd/unittests/SourceCodeTests.cpp

Modified: clang-tools-extra/trunk/clangd/SourceCode.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/SourceCode.cpp?rev=368058&r1=368057&r2=368058&view=diff
==
--- clang-tools-extra/trunk/clangd/SourceCode.cpp (original)
+++ clang-tools-extra/trunk/clangd/SourceCode.cpp Tue Aug  6 10:01:12 2019
@@ -296,14 +296,52 @@ static SourceRange unionTokenRange(Sourc
  E1 < E2 ? R2.getEnd() : R1.getEnd());
 }
 
-// Returns the tokenFileRange for a given Location as a Token Range
+// Check if two locations have the same file id.
+static bool inSameFile(SourceLocation Loc1, SourceLocation Loc2,
+   const SourceManager &SM) {
+  return SM.getFileID(Loc1) == SM.getFileID(Loc2);
+}
+
+// Find an expansion range (not necessarily immediate) the ends of which are in
+// the same file id.
+static SourceRange
+getExpansionTokenRangeInSameFile(SourceLocation Loc, const SourceManager &SM,
+ const LangOptions &LangOpts) {
+  SourceRange ExpansionRange =
+  toTokenRange(SM.getImmediateExpansionRange(Loc), SM, LangOpts);
+  // Fast path for most common cases.
+  if (inSameFile(ExpansionRange.getBegin(), ExpansionRange.getEnd(), SM))
+return ExpansionRange;
+  // Record the stack of expansion locations for the beginning, keyed by 
FileID.
+  llvm::DenseMap BeginExpansions;
+  for (SourceLocation Begin = ExpansionRange.getBegin(); Begin.isValid();
+   Begin = Begin.isFileID()
+   ? SourceLocation()
+   : SM.getImmediateExpansionRange(Begin).getBegin()) {
+BeginExpansions[SM.getFileID(Begin)] = Begin;
+  }
+  // Move up the stack of expansion locations for the end until we find the
+  // location in BeginExpansions with that has the same file id.
+  for (SourceLocation End = ExpansionRange.getEnd(); End.isValid();
+   End = End.isFileID() ? SourceLocation()
+: toTokenRange(SM.getImmediateExpansionRange(End),
+   SM, LangOpts)
+  .getEnd()) {
+auto It = BeginExpansions.find(SM.getFileID(End));
+if (It != BeginExpansions.end())
+  return {It->second, End};
+  }
+  llvm_unreachable(
+  "We should able to find a common ancestor in the expansion tree.");
+}
+// Returns the file range for a given Location as a Token Range
 // This is quite similar to getFileLoc in SourceManager as both use
 // getImmediateExpansionRange and getImmediateSpellingLoc (for macro IDs).
 // However:
 // - We want to maintain the full range information as we move from one file to
 //   the next. getFileLoc only uses the BeginLoc of getImmediateExpansionRange.
-// - We want to split '>>' tokens as the lexer parses the '>>' in template
-//   instantiations as a '>>' instead of a '>'.
+// - We want to split '>>' tokens as the lexer parses the '>>' in nested
+//   template instantiations as a '>>' instead of two '>'s.
 // There is also getExpansionRange but it simply calls
 // getImmediateExpansionRange on the begin and ends separately which is wrong.
 static SourceRange getTokenFileRange(SourceLocation Loc,
@@ -311,16 +349,19 @@ static SourceRange getTokenFileRange(Sou
  const LangOptions &LangOpts) {
   SourceRange FileRange = Loc;
   while (!FileRange.getBegin().isFileID()) {
-assert(!FileRange.getEnd().isFileID() &&
-   "Both Begin and End should be MacroIDs.");
 if (SM.isMacroArgExpansion(FileRange.getBegin())) {
-  FileRange.setBegin(SM.getImmediateSpellingLoc(FileRange.getBegin()));
-  FileRange.setEnd(SM.getImmediateSpellingLoc(FileRange.getEnd()));
+  FileRange = unionTokenRange(
+  SM.getImmediateSpellingLoc(FileRange.getBegin()),
+  SM.getImmediateSpellingLoc(FileRange.getEnd()), SM, LangOpts);
+  assert(inSameFile(FileRange.getBegin(), FileRange.getEnd(), SM));
 } else {
-  SourceRange ExpansionRangeForBegin = toTokenRange(
-  SM.getImmediateExpansionRange(FileRange.getBegin()), SM, LangOpts);
-  SourceRange ExpansionRangeForEnd = toTokenRange(
-  SM.getImmediateExpansionRange(FileRange.getEnd()), SM, LangOpts);
+  SourceRange ExpansionRangeForBegin =
+  getExpansionTokenRangeInSameFile(FileRange.getBegin(), 

r368267 - [Extract] Fixed SemicolonExtractionPolicy for SwitchStmt and SwitchCase

2019-08-08 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Thu Aug  8 01:37:49 2019
New Revision: 368267

URL: http://llvm.org/viewvc/llvm-project?rev=368267&view=rev
Log:
[Extract] Fixed SemicolonExtractionPolicy for SwitchStmt and SwitchCase

Reviewers: arphaman, sammccall

Subscribers: dexonsmith, cfe-commits

Tags: #clang

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

Modified:
cfe/trunk/lib/Tooling/Refactoring/Extract/SourceExtraction.cpp
cfe/trunk/test/Refactor/Extract/ExtractionSemicolonPolicy.cpp

Modified: cfe/trunk/lib/Tooling/Refactoring/Extract/SourceExtraction.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Refactoring/Extract/SourceExtraction.cpp?rev=368267&r1=368266&r2=368267&view=diff
==
--- cfe/trunk/lib/Tooling/Refactoring/Extract/SourceExtraction.cpp (original)
+++ cfe/trunk/lib/Tooling/Refactoring/Extract/SourceExtraction.cpp Thu Aug  8 
01:37:49 2019
@@ -40,8 +40,11 @@ bool isSemicolonRequiredAfter(const Stmt
 return isSemicolonRequiredAfter(CXXFor->getBody());
   if (const auto *ObjCFor = dyn_cast(S))
 return isSemicolonRequiredAfter(ObjCFor->getBody());
+  if(const auto *Switch = dyn_cast(S))
+return isSemicolonRequiredAfter(Switch->getBody());
+  if(const auto *Case = dyn_cast(S))
+return isSemicolonRequiredAfter(Case->getSubStmt());
   switch (S->getStmtClass()) {
-  case Stmt::SwitchStmtClass:
   case Stmt::CXXTryStmtClass:
   case Stmt::ObjCAtSynchronizedStmtClass:
   case Stmt::ObjCAutoreleasePoolStmtClass:

Modified: cfe/trunk/test/Refactor/Extract/ExtractionSemicolonPolicy.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Refactor/Extract/ExtractionSemicolonPolicy.cpp?rev=368267&r1=368266&r2=368267&view=diff
==
--- cfe/trunk/test/Refactor/Extract/ExtractionSemicolonPolicy.cpp (original)
+++ cfe/trunk/test/Refactor/Extract/ExtractionSemicolonPolicy.cpp Thu Aug  8 
01:37:49 2019
@@ -64,6 +64,7 @@ void extractStatementNotSemiSwitch() {
 // CHECK-NEXT: extracted();{{$}}
 // CHECK-NEXT: }
 
+
 void extractStatementNotSemiWhile() {
   /*range eextract=->+2:4*/while (true) {
 int x = 0;
@@ -190,3 +191,15 @@ void careForNonCompoundSemicolons2() {
 // CHECK-NEXT: extracted();{{$}}
 // CHECK-NEXT: //
 // CHECK-NEXT: }
+
+void careForSwitchSemicolon() {
+  /*range mextract=->+0:51*/switch(0) default: break;
+}
+// CHECK: 1 'mextract' results:
+// CHECK:  static void extracted() {
+// CHECK-NEXT: switch(0) default: break;{{$}}
+// CHECK-NEXT: }{{[[:space:]].*}}
+// CHECK-NEXT: void careForSwitchSemicolon() {
+// CHECK-NEXT: extracted();{{$}}
+// CHECK-NEXT: }
+


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


r368743 - [Refactor] Moving SourceExtraction header from lib to include

2019-08-13 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Tue Aug 13 13:21:00 2019
New Revision: 368743

URL: http://llvm.org/viewvc/llvm-project?rev=368743&view=rev
Log:
[Refactor] Moving SourceExtraction header from lib to include

Summary:
- Moved the SourceExtraction header from lib to include so that it can be used 
in clangd.

Reviewers: arphaman

Subscribers: ilya-biryukov, dexonsmith, kadircet, cfe-commits

Tags: #clang

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

Added:
cfe/trunk/include/clang/Tooling/Refactoring/Extract/SourceExtraction.h   
(with props)
Removed:
cfe/trunk/lib/Tooling/Refactoring/Extract/SourceExtraction.h
Modified:
cfe/trunk/lib/Tooling/Refactoring/Extract/Extract.cpp
cfe/trunk/lib/Tooling/Refactoring/Extract/SourceExtraction.cpp

Added: cfe/trunk/include/clang/Tooling/Refactoring/Extract/SourceExtraction.h
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/Refactoring/Extract/SourceExtraction.h?rev=368743&view=auto
==
--- cfe/trunk/include/clang/Tooling/Refactoring/Extract/SourceExtraction.h 
(added)
+++ cfe/trunk/include/clang/Tooling/Refactoring/Extract/SourceExtraction.h Tue 
Aug 13 13:21:00 2019
@@ -0,0 +1,51 @@
+//===--- SourceExtraction.cpp - Clang refactoring library 
-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLING_REFACTORING_EXTRACT_SOURCE_EXTRACTION_H
+#define LLVM_CLANG_TOOLING_REFACTORING_EXTRACT_SOURCE_EXTRACTION_H
+
+#include "clang/Basic/LLVM.h"
+
+namespace clang {
+
+class LangOptions;
+class SourceManager;
+class SourceRange;
+class Stmt;
+
+namespace tooling {
+
+/// Determines which semicolons should be inserted during extraction.
+class ExtractionSemicolonPolicy {
+public:
+  bool isNeededInExtractedFunction() const {
+return IsNeededInExtractedFunction;
+  }
+
+  bool isNeededInOriginalFunction() const { return IsNeededInOriginalFunction; 
}
+
+  /// Returns the semicolon insertion policy that is needed for extraction of
+  /// the given statement from the given source range.
+  static ExtractionSemicolonPolicy compute(const Stmt *S,
+   SourceRange &ExtractedRange,
+   const SourceManager &SM,
+   const LangOptions &LangOpts);
+
+private:
+  ExtractionSemicolonPolicy(bool IsNeededInExtractedFunction,
+bool IsNeededInOriginalFunction)
+  : IsNeededInExtractedFunction(IsNeededInExtractedFunction),
+IsNeededInOriginalFunction(IsNeededInOriginalFunction) {}
+  bool IsNeededInExtractedFunction;
+  bool IsNeededInOriginalFunction;
+};
+
+} // end namespace tooling
+} // end namespace clang
+
+#endif //LLVM_CLANG_TOOLING_REFACTORING_EXTRACT_SOURCE_EXTRACTION_H

Propchange: 
cfe/trunk/include/clang/Tooling/Refactoring/Extract/SourceExtraction.h
--
svn:executable = *

Modified: cfe/trunk/lib/Tooling/Refactoring/Extract/Extract.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Refactoring/Extract/Extract.cpp?rev=368743&r1=368742&r2=368743&view=diff
==
--- cfe/trunk/lib/Tooling/Refactoring/Extract/Extract.cpp (original)
+++ cfe/trunk/lib/Tooling/Refactoring/Extract/Extract.cpp Tue Aug 13 13:21:00 
2019
@@ -13,12 +13,12 @@
 
//===--===//
 
 #include "clang/Tooling/Refactoring/Extract/Extract.h"
-#include "SourceExtraction.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/DeclCXX.h"
 #include "clang/AST/Expr.h"
 #include "clang/AST/ExprObjC.h"
 #include "clang/Rewrite/Core/Rewriter.h"
+#include "clang/Tooling/Refactoring/Extract/SourceExtraction.h"
 
 namespace clang {
 namespace tooling {

Modified: cfe/trunk/lib/Tooling/Refactoring/Extract/SourceExtraction.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Refactoring/Extract/SourceExtraction.cpp?rev=368743&r1=368742&r2=368743&view=diff
==
--- cfe/trunk/lib/Tooling/Refactoring/Extract/SourceExtraction.cpp (original)
+++ cfe/trunk/lib/Tooling/Refactoring/Extract/SourceExtraction.cpp Tue Aug 13 
13:21:00 2019
@@ -6,7 +6,7 @@
 //
 
//===--===//
 
-#include "SourceExtraction.h"
+#include "clang/Tooling/Refactoring/Extract/SourceExtraction.h"
 #include "clang/AST/Stmt.h"
 #include "clang/AST/StmtCXX.h"
 #include "clang/AST/StmtObjC.h"

Removed: cfe/trunk/lib/Tooling/Refa

[clang-tools-extra] r368841 - [Clangd] NFC: Fixed comment typo

2019-08-14 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Wed Aug 14 05:40:09 2019
New Revision: 368841

URL: http://llvm.org/viewvc/llvm-project?rev=368841&view=rev
Log:
[Clangd] NFC: Fixed comment typo

Modified:
clang-tools-extra/trunk/clangd/unittests/TweakTesting.h

Modified: clang-tools-extra/trunk/clangd/unittests/TweakTesting.h
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/TweakTesting.h?rev=368841&r1=368840&r2=368841&view=diff
==
--- clang-tools-extra/trunk/clangd/unittests/TweakTesting.h (original)
+++ clang-tools-extra/trunk/clangd/unittests/TweakTesting.h Wed Aug 14 05:40:09 
2019
@@ -25,7 +25,7 @@ namespace clangd {
 //   Header = R"cpp(
 // namespace foo { template class X{}; }
 // using namespace foo;
-//   cpp)";
+//   )cpp";
 //   Context = Function;
 //   EXPECT_THAT(apply("[[auto]] X = foo();"),
 //   "foo X = foohttps://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


r368850 - [Tooling] Added DeclStmtClass to ExtractionSemicolonPolicy

2019-08-14 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Wed Aug 14 06:37:39 2019
New Revision: 368850

URL: http://llvm.org/viewvc/llvm-project?rev=368850&view=rev
Log:
[Tooling] Added DeclStmtClass to ExtractionSemicolonPolicy

Since the DeclStmt range includes the semicolon, it doesn't need a
semicolon at the end during extraction

Modified:
cfe/trunk/lib/Tooling/Refactoring/Extract/SourceExtraction.cpp
cfe/trunk/test/Refactor/Extract/ExtractionSemicolonPolicy.cpp

Modified: cfe/trunk/lib/Tooling/Refactoring/Extract/SourceExtraction.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Refactoring/Extract/SourceExtraction.cpp?rev=368850&r1=368849&r2=368850&view=diff
==
--- cfe/trunk/lib/Tooling/Refactoring/Extract/SourceExtraction.cpp (original)
+++ cfe/trunk/lib/Tooling/Refactoring/Extract/SourceExtraction.cpp Wed Aug 14 
06:37:39 2019
@@ -45,6 +45,7 @@ bool isSemicolonRequiredAfter(const Stmt
   if(const auto *Case = dyn_cast(S))
 return isSemicolonRequiredAfter(Case->getSubStmt());
   switch (S->getStmtClass()) {
+  case Stmt::DeclStmtClass:
   case Stmt::CXXTryStmtClass:
   case Stmt::ObjCAtSynchronizedStmtClass:
   case Stmt::ObjCAutoreleasePoolStmtClass:

Modified: cfe/trunk/test/Refactor/Extract/ExtractionSemicolonPolicy.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Refactor/Extract/ExtractionSemicolonPolicy.cpp?rev=368850&r1=368849&r2=368850&view=diff
==
--- cfe/trunk/test/Refactor/Extract/ExtractionSemicolonPolicy.cpp (original)
+++ cfe/trunk/test/Refactor/Extract/ExtractionSemicolonPolicy.cpp Wed Aug 14 
06:37:39 2019
@@ -193,7 +193,7 @@ void careForNonCompoundSemicolons2() {
 // CHECK-NEXT: }
 
 void careForSwitchSemicolon() {
-  /*range mextract=->+0:51*/switch(0) default: break;
+  /*range mextract=->+0:53*/switch(0) default: break;
 }
 // CHECK: 1 'mextract' results:
 // CHECK:  static void extracted() {
@@ -203,3 +203,14 @@ void careForSwitchSemicolon() {
 // CHECK-NEXT: extracted();{{$}}
 // CHECK-NEXT: }
 
+void extractStatementNotSemiDecl() {
+  /*range nextract=->+0:38*/int x = 5;
+}
+// CHECK: 1 'nextract' results:
+// CHECK:  static void extracted() {
+// CHECK-NEXT: int x = 5;{{$}}
+// CHECK-NEXT: }{{[[:space:]].*}}
+// CHECK-NEXT: void extractStatementNotSemiDecl() {
+// CHECK-NEXT: extracted();{{$}}
+// CHECK-NEXT: }
+


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


[clang-tools-extra] r364809 - Summary: [Clangd] Added hidden command line option -tweaks to specify which tweaks to enable

2019-07-01 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Mon Jul  1 09:55:29 2019
New Revision: 364809

URL: http://llvm.org/viewvc/llvm-project?rev=364809&view=rev
Log:
Summary: [Clangd] Added hidden command line option -tweaks to specify which 
tweaks to enable

- Only for development purposes
- Disabled tweaks in fixits-duplications test
Reviewers: sammccall, kadircet

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

Tags: #clang

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

Modified:
clang-tools-extra/trunk/clangd/ClangdServer.cpp
clang-tools-extra/trunk/clangd/ClangdServer.h
clang-tools-extra/trunk/clangd/test/fixits-duplication.test
clang-tools-extra/trunk/clangd/tool/ClangdMain.cpp

Modified: clang-tools-extra/trunk/clangd/ClangdServer.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdServer.cpp?rev=364809&r1=364808&r2=364809&view=diff
==
--- clang-tools-extra/trunk/clangd/ClangdServer.cpp (original)
+++ clang-tools-extra/trunk/clangd/ClangdServer.cpp Mon Jul  1 09:55:29 2019
@@ -102,6 +102,7 @@ ClangdServer::ClangdServer(const GlobalC
   GetClangTidyOptions(Opts.GetClangTidyOptions),
   SuggestMissingIncludes(Opts.SuggestMissingIncludes),
   EnableHiddenFeatures(Opts.HiddenFeatures),
+  TweakFilter(Opts.TweakFilter),
   WorkspaceRoot(Opts.WorkspaceRoot),
   // Pass a callback into `WorkScheduler` to extract symbols from a newly
   // parsed file and rebuild the file index synchronously each time an AST
@@ -333,7 +334,7 @@ void ClangdServer::enumerateTweaks(PathR
   return CB(Selection.takeError());
 std::vector Res;
 for (auto &T : prepareTweaks(*Selection)) {
-  if (T->hidden() && !EnableHiddenFeatures)
+  if (!TweakFilter(T->id()) || (T->hidden() && !EnableHiddenFeatures))
 continue;
   Res.push_back({T->id(), T->title(), T->intent()});
 }

Modified: clang-tools-extra/trunk/clangd/ClangdServer.h
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/ClangdServer.h?rev=364809&r1=364808&r2=364809&view=diff
==
--- clang-tools-extra/trunk/clangd/ClangdServer.h (original)
+++ clang-tools-extra/trunk/clangd/ClangdServer.h Mon Jul  1 09:55:29 2019
@@ -140,6 +140,9 @@ public:
 
 /// Enable semantic highlighting features.
 bool SemanticHighlighting = false;
+
+/// Returns true if the StringRef is a tweak that should be enabled
+std::function TweakFilter = [](llvm::StringRef 
TweakToSearch) {return true;};
   };
   // Sensible default options for use in tests.
   // Features like indexing must be enabled if desired.
@@ -313,7 +316,9 @@ private:
   // can be caused by missing includes (e.g. member access in incomplete type).
   bool SuggestMissingIncludes = false;
   bool EnableHiddenFeatures = false;
-  
+   
+  std::function TweakFilter;
+
   // GUARDED_BY(CachedCompletionFuzzyFindRequestMutex)
   llvm::StringMap>
   CachedCompletionFuzzyFindRequestByFile;

Modified: clang-tools-extra/trunk/clangd/test/fixits-duplication.test
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/test/fixits-duplication.test?rev=364809&r1=364808&r2=364809&view=diff
==
--- clang-tools-extra/trunk/clangd/test/fixits-duplication.test (original)
+++ clang-tools-extra/trunk/clangd/test/fixits-duplication.test Mon Jul  1 
09:55:29 2019
@@ -1,4 +1,4 @@
-# RUN: clangd -lit-test 
-clang-tidy-checks=modernize-use-nullptr,hicpp-use-nullptr < %s | FileCheck 
-strict-whitespace %s
+# RUN: clangd -lit-test 
-clang-tidy-checks=modernize-use-nullptr,hicpp-use-nullptr -tweaks="" < %s | 
FileCheck -strict-whitespace %s
 
{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{"textDocument":{"codeAction":{"codeActionLiteralSupport":{,"trace":"off"}}
 ---
 
{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"test:///foo.cpp","languageId":"cpp","version":1,"text":"void
 foo() { char* p = 0; }"}}}

Modified: clang-tools-extra/trunk/clangd/tool/ClangdMain.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/tool/ClangdMain.cpp?rev=364809&r1=364808&r2=364809&view=diff
==
--- clang-tools-extra/trunk/clangd/tool/ClangdMain.cpp (original)
+++ clang-tools-extra/trunk/clangd/tool/ClangdMain.cpp Mon Jul  1 09:55:29 2019
@@ -278,6 +278,12 @@ static llvm::cl::list Query
 "/usr/bin/**/clang-*,/path/to/repo/**/g++-*"),
 llvm::cl::CommaSeparated);
 
+static llvm::cl::list TweakList(
+"tweaks",
+llvm::cl::desc(
+"Specify a list of Tweaks to enable (only for clangd developers)."),
+llvm::cl::Hidden, llvm::cl::CommaSeparated);
+
 namespace {
 
 /// \brief S

[clang-tools-extra] r365453 - dummy variable extraction on a function scope

2019-07-09 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Tue Jul  9 03:12:26 2019
New Revision: 365453

URL: http://llvm.org/viewvc/llvm-project?rev=365453&view=rev
Log:
dummy variable extraction on a function scope

Summary:
- Added extraction to a dummy variable
- using auto for the dummy variable type for now
- Works on a function scope
- Adding braces to create a compound statement not supported yet
- added unit tests

Reviewers: sammccall, kadircet

Subscribers: mgorny, jkorous, arphaman, cfe-commits

Tags: #clang

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

Added:
clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp
Modified:
clang-tools-extra/trunk/clangd/refactor/tweaks/CMakeLists.txt
clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp

Modified: clang-tools-extra/trunk/clangd/refactor/tweaks/CMakeLists.txt
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/refactor/tweaks/CMakeLists.txt?rev=365453&r1=365452&r2=365453&view=diff
==
--- clang-tools-extra/trunk/clangd/refactor/tweaks/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clangd/refactor/tweaks/CMakeLists.txt Tue Jul  9 
03:12:26 2019
@@ -17,6 +17,7 @@ add_clang_library(clangDaemonTweaks OBJE
   ExpandMacro.cpp
   RawStringLiteral.cpp
   SwapIfBranches.cpp
+  ExtractVariable.cpp
 
   LINK_LIBS
   clangAST

Added: clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp?rev=365453&view=auto
==
--- clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp (added)
+++ clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp Tue Jul  
9 03:12:26 2019
@@ -0,0 +1,243 @@
+//===--- ExtractVariable.cpp *- C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+#include "ClangdUnit.h"
+#include "Logger.h"
+#include "Protocol.h"
+#include "Selection.h"
+#include "SourceCode.h"
+#include "refactor/Tweak.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/Expr.h"
+#include "clang/AST/OperationKinds.h"
+#include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/AST/Stmt.h"
+#include "clang/AST/StmtCXX.h"
+#include "clang/Basic/LangOptions.h"
+#include "clang/Basic/SourceLocation.h"
+#include "clang/Basic/SourceManager.h"
+#include "clang/Tooling/Core/Replacement.h"
+#include "llvm/ADT/None.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Casting.h"
+#include "llvm/Support/Error.h"
+
+namespace clang {
+namespace clangd {
+namespace {
+// information regarding the Expr that is being extracted
+class ExtractionContext {
+public:
+  ExtractionContext(const SelectionTree::Node *Node, const SourceManager &SM,
+const ASTContext &Ctx);
+  const clang::Expr *getExpr() const { return Expr; }
+  const SelectionTree::Node *getExprNode() const { return ExprNode; }
+  bool isExtractable() const { return Extractable; }
+  // Generate Replacement for replacing selected expression with given VarName
+  tooling::Replacement replaceWithVar(llvm::StringRef VarName) const;
+  // Generate Replacement for declaring the selected Expr as a new variable
+  tooling::Replacement insertDeclaration(llvm::StringRef VarName) const;
+
+private:
+  bool Extractable = false;
+  const clang::Expr *Expr;
+  const SelectionTree::Node *ExprNode;
+  // Stmt before which we will extract
+  const clang::Stmt *InsertionPoint = nullptr;
+  const SourceManager &SM;
+  const ASTContext &Ctx;
+  // Decls referenced in the Expr
+  std::vector ReferencedDecls;
+  // returns true if the Expr doesn't reference any variable declared in scope
+  bool exprIsValidOutside(const clang::Stmt *Scope) const;
+  // computes the Stmt before which we will extract out Expr
+  const clang::Stmt *computeInsertionPoint() const;
+};
+
+// Returns all the Decls referenced inside the given Expr
+static std::vector
+computeReferencedDecls(const clang::Expr *Expr) {
+  // RAV subclass to find all DeclRefs in a given Stmt
+  class FindDeclRefsVisitor
+  : public clang::RecursiveASTVisitor {
+  public:
+std::vector ReferencedDecls;
+bool VisitDeclRefExpr(DeclRefExpr *DeclRef) { // NOLINT
+  ReferencedDecls.push_back(DeclRef->getDecl());
+  return true;
+}
+  };
+  FindDeclRefsVisitor Visitor;
+  Visitor.TraverseStmt(const_cast(dyn_cast(Expr)));
+  return Visitor.ReferencedDecls;
+}
+
+// An expr is not extractable if it's null or an expression of type void
+// FIXME: Ignore assignment (a = 1) Expr since it is extracted as dummy 

[clang-tools-extra] r365460 - Fixed assertion

2019-07-09 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Tue Jul  9 03:30:18 2019
New Revision: 365460

URL: http://llvm.org/viewvc/llvm-project?rev=365460&view=rev
Log:
Fixed assertion

Modified:
clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp

Modified: clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp?rev=365460&r1=365459&r2=365460&view=diff
==
--- clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp 
(original)
+++ clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractVariable.cpp Tue Jul  
9 03:30:18 2019
@@ -181,7 +181,7 @@ tooling::Replacement
 ExtractionContext::insertDeclaration(llvm::StringRef VarName) const {
   const llvm::Optional ExtractionRng =
   toHalfOpenFileRange(SM, Ctx.getLangOpts(), getExpr()->getSourceRange());
-  assert(ExractionRng && "ExtractionRng should not be null");
+  assert(ExtractionRng && "ExtractionRng should not be null");
   llvm::StringRef ExtractionCode = toSourceCode(SM, *ExtractionRng);
   const SourceLocation InsertionLoc =
   toHalfOpenFileRange(SM, Ctx.getLangOpts(),


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


[clang-tools-extra] r372627 - [Clang-doc] NFC: Fixed link to llvm bugs in documentation

2019-09-23 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Mon Sep 23 09:55:13 2019
New Revision: 372627

URL: http://llvm.org/viewvc/llvm-project?rev=372627&view=rev
Log:
[Clang-doc] NFC: Fixed link to llvm bugs in documentation

Modified:
clang-tools-extra/trunk/docs/clang-doc.rst

Modified: clang-tools-extra/trunk/docs/clang-doc.rst
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-doc.rst?rev=372627&r1=372626&r2=372627&view=diff
==
--- clang-tools-extra/trunk/docs/clang-doc.rst (original)
+++ clang-tools-extra/trunk/docs/clang-doc.rst Mon Sep 23 09:55:13 2019
@@ -12,7 +12,7 @@ source code and comments.
 
 The tool is in a very early development stage, so you might encounter bugs and
 crashes. Submitting reports with information about how to reproduce the issue
-to `the LLVM bugtracker `_ will definitely help the
+to `the LLVM bugtracker `_ will definitely help the
 project. If you have any ideas or suggestions, please to put a feature request
 there.
 


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


[clang-tools-extra] r373471 - [Clangd] Ensure children are always RootStmt in ExtractFunction (Fixes #153)

2019-10-02 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Wed Oct  2 06:51:06 2019
New Revision: 373471

URL: http://llvm.org/viewvc/llvm-project?rev=373471&view=rev
Log:
[Clangd] Ensure children are always RootStmt in ExtractFunction (Fixes #153)

Summary:
We weren't always checking if children are RootStmts in ExtractFunction.

For `void f([[int a]]);`, the ParmVarDecl appeared as a RootStmt since
we didn't perform the check and ended up being casted to a (null) Stmt.

Reviewers: sammccall, kadircet

Subscribers: kristof.beyls, ilya-biryukov, MaskRay, jkorous, arphaman, 
usaxena95, cfe-commits

Tags: #clang

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

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

Modified: clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractFunction.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractFunction.cpp?rev=373471&r1=373470&r2=373471&view=diff
==
--- clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractFunction.cpp 
(original)
+++ clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractFunction.cpp Wed Oct  
2 06:51:06 2019
@@ -104,6 +104,11 @@ bool isRootStmt(const Node *N) {
 }
 
 // Returns the (unselected) parent of all RootStmts given the commonAncestor.
+// Returns null if:
+// 1. any node is partially selected
+// 2. If all completely selected nodes don't have the same common parent
+// 3. Any child of Parent isn't a RootStmt.
+// Returns null if any child is not a RootStmt.
 // We only support extraction of RootStmts since it allows us to extract 
without
 // having to change the selection range. Also, this means that any scope that
 // begins in selection range, ends in selection range and any scope that begins
@@ -111,25 +116,30 @@ bool isRootStmt(const Node *N) {
 const Node *getParentOfRootStmts(const Node *CommonAnc) {
   if (!CommonAnc)
 return nullptr;
+  const Node *Parent = nullptr;
   switch (CommonAnc->Selected) {
   case SelectionTree::Selection::Unselected:
 // Typicaly a block, with the { and } unselected, could also be ForStmt etc
 // Ensure all Children are RootStmts.
-return llvm::all_of(CommonAnc->Children, isRootStmt) ? CommonAnc : nullptr;
+Parent = CommonAnc;
+break;
   case SelectionTree::Selection::Partial:
 // Only a fully-selected single statement can be selected.
 return nullptr;
   case SelectionTree::Selection::Complete:
 // If the Common Ancestor is completely selected, then it's a root 
statement
 // and its parent will be unselected.
-const Node *Parent = CommonAnc->Parent;
+Parent = CommonAnc->Parent;
 // If parent is a DeclStmt, even though it's unselected, we consider it a
 // root statement and return its parent. This is done because the VarDecls
 // claim the entire selection range of the Declaration and DeclStmt is
 // always unselected.
-return Parent->ASTNode.get() ? Parent->Parent : Parent;
+if (Parent->ASTNode.get())
+  Parent = Parent->Parent;
+break;
   }
-  llvm_unreachable("Unhandled SelectionTree::Selection enum");
+  // Ensure all Children are RootStmts.
+  return llvm::all_of(Parent->Children, isRootStmt) ? Parent : nullptr;
 }
 
 // The ExtractionZone class forms a view of the code wrt Zone.

Modified: clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp?rev=373471&r1=373470&r2=373471&view=diff
==
--- clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp (original)
+++ clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp Wed Oct  2 06:51:06 
2019
@@ -629,6 +629,9 @@ void f(const int c) {
 F ([[int x = 0;]])
   )cpp";
   EXPECT_EQ(apply(MacroFailInput), "unavailable");
+
+  // Shouldn't crash.
+  EXPECT_EQ(apply("void f([[int a]]);"), "unavailable");
 }
 
 TEST_F(ExtractFunctionTest, ControlFlow) {


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


[clang-tools-extra] r373472 - [Clangd] ExtractFunction: Don't extract body of enclosing function.

2019-10-02 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Wed Oct  2 06:52:14 2019
New Revision: 373472

URL: http://llvm.org/viewvc/llvm-project?rev=373472&view=rev
Log:
[Clangd] ExtractFunction: Don't extract body of enclosing function.

Summary:
This patch disable extraction of the body of the enclosing function.
`void f() [[{}]]`

Extracting this CompoundStmt would leave the enclosing function without
a body.

Reviewers: sammccall, kadircet

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

Tags: #clang

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

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

Modified: clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractFunction.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractFunction.cpp?rev=373472&r1=373471&r2=373472&view=diff
==
--- clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractFunction.cpp 
(original)
+++ clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractFunction.cpp Wed Oct  
2 06:52:14 2019
@@ -225,10 +225,24 @@ computeEnclosingFuncRange(const Function
   return toHalfOpenFileRange(SM, LangOpts, 
EnclosingFunction->getSourceRange());
 }
 
+// returns true if Child can be a single RootStmt being extracted from
+// EnclosingFunc.
+bool validSingleChild(const Node *Child, const FunctionDecl *EnclosingFunc) {
+  // Don't extract expressions.
+  // FIXME: We should extract expressions that are "statements" i.e. not
+  // subexpressions
+  if (Child->ASTNode.get())
+return false;
+  // Extracting the body of EnclosingFunc would remove it's definition.
+  assert(EnclosingFunc->hasBody() &&
+ "We should always be extracting from a function body.");
+  if (Child->ASTNode.get() == EnclosingFunc->getBody())
+return false;
+  return true;
+}
+
 // FIXME: Check we're not extracting from the initializer/condition of a 
control
 // flow structure.
-// FIXME: Check that we don't extract the compound statement of the
-// enclosingFunction.
 llvm::Optional findExtractionZone(const Node *CommonAnc,
   const SourceManager &SM,
   const LangOptions &LangOpts) 
{
@@ -236,15 +250,14 @@ llvm::Optional findExtra
   ExtZone.Parent = getParentOfRootStmts(CommonAnc);
   if (!ExtZone.Parent || ExtZone.Parent->Children.empty())
 return llvm::None;
-  // Don't extract expressions.
-  // FIXME: We should extract expressions that are "statements" i.e. not
-  // subexpressions
-  if (ExtZone.Parent->Children.size() == 1 &&
-  ExtZone.getLastRootStmt()->ASTNode.get())
-return llvm::None;
   ExtZone.EnclosingFunction = findEnclosingFunction(ExtZone.Parent);
   if (!ExtZone.EnclosingFunction)
 return llvm::None;
+  // When there is a single RootStmt, we must check if it's valid for
+  // extraction.
+  if (ExtZone.Parent->Children.size() == 1 &&
+  !validSingleChild(ExtZone.getLastRootStmt(), ExtZone.EnclosingFunction))
+return llvm::None;
   if (auto FuncRange =
   computeEnclosingFuncRange(ExtZone.EnclosingFunction, SM, LangOpts))
 ExtZone.EnclosingFuncRange = *FuncRange;

Modified: clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp?rev=373472&r1=373471&r2=373472&view=diff
==
--- clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp (original)
+++ clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp Wed Oct  2 06:52:14 
2019
@@ -632,6 +632,13 @@ void f(const int c) {
 
   // Shouldn't crash.
   EXPECT_EQ(apply("void f([[int a]]);"), "unavailable");
+  // Don't extract if we select the entire function body (CompoundStmt).
+  std::string CompoundFailInput = R"cpp(
+void f() [[{
+  int a;
+}]]
+  )cpp";
+  EXPECT_EQ(apply(CompoundFailInput), "unavailable");
 }
 
 TEST_F(ExtractFunctionTest, ControlFlow) {


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


[clang-tools-extra] r369666 - Fixed Missing Expected error handling

2019-08-22 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Thu Aug 22 09:42:42 2019
New Revision: 369666

URL: http://llvm.org/viewvc/llvm-project?rev=369666&view=rev
Log:
Fixed Missing Expected error handling

Modified:
clang-tools-extra/trunk/clangd/unittests/TweakTesting.cpp

Modified: clang-tools-extra/trunk/clangd/unittests/TweakTesting.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/TweakTesting.cpp?rev=369666&r1=369665&r2=369666&view=diff
==
--- clang-tools-extra/trunk/clangd/unittests/TweakTesting.cpp (original)
+++ clang-tools-extra/trunk/clangd/unittests/TweakTesting.cpp Thu Aug 22 
09:42:42 2019
@@ -89,8 +89,10 @@ std::string TweakTest::apply(llvm::Strin
   Tweak::Selection S(AST, Selection.first, Selection.second);
 
   auto T = prepareTweak(TweakID, S);
-  if (!T)
+  if (!T) {
+llvm::toString(T.takeError());
 return "unavailable";
+  }
   llvm::Expected Result = (*T)->apply(S);
   if (!Result)
 return "fail: " + llvm::toString(Result.takeError());


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


[clang-tools-extra] r369676 - [Clangd] Tweaktesting replace toString with consumeError

2019-08-22 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Thu Aug 22 10:36:31 2019
New Revision: 369676

URL: http://llvm.org/viewvc/llvm-project?rev=369676&view=rev
Log:
[Clangd] Tweaktesting replace toString with consumeError

Modified:
clang-tools-extra/trunk/clangd/unittests/TweakTesting.cpp

Modified: clang-tools-extra/trunk/clangd/unittests/TweakTesting.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/TweakTesting.cpp?rev=369676&r1=369675&r2=369676&view=diff
==
--- clang-tools-extra/trunk/clangd/unittests/TweakTesting.cpp (original)
+++ clang-tools-extra/trunk/clangd/unittests/TweakTesting.cpp Thu Aug 22 
10:36:31 2019
@@ -90,7 +90,7 @@ std::string TweakTest::apply(llvm::Strin
 
   auto T = prepareTweak(TweakID, S);
   if (!T) {
-llvm::toString(T.takeError());
+llvm::consumeError(T.takeError());
 return "unavailable";
   }
   llvm::Expected Result = (*T)->apply(S);


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


[clang-tools-extra] r370249 - [Clangd] Initial version of ExtractFunction

2019-08-28 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Wed Aug 28 12:34:17 2019
New Revision: 370249

URL: http://llvm.org/viewvc/llvm-project?rev=370249&view=rev
Log:
[Clangd] Initial version of ExtractFunction

Summary:
- Only works for extraction from free functions
- Basic analysis of the code being extracted.
- Extract to void function
- Bail out if extracting a return, continue or break.
- Doesn't hoist decls yet

Reviewers: kadircet, sammccall

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

Tags: #clang

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

Added:
clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractFunction.cpp
Modified:
clang-tools-extra/trunk/clangd/refactor/tweaks/CMakeLists.txt
clang-tools-extra/trunk/clangd/unittests/TweakTests.cpp

Modified: clang-tools-extra/trunk/clangd/refactor/tweaks/CMakeLists.txt
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/refactor/tweaks/CMakeLists.txt?rev=370249&r1=370248&r2=370249&view=diff
==
--- clang-tools-extra/trunk/clangd/refactor/tweaks/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clangd/refactor/tweaks/CMakeLists.txt Wed Aug 28 
12:34:17 2019
@@ -16,6 +16,7 @@ add_clang_library(clangDaemonTweaks OBJE
   DumpAST.cpp
   ExpandAutoType.cpp
   ExpandMacro.cpp
+  ExtractFunction.cpp
   ExtractVariable.cpp
   RawStringLiteral.cpp
   SwapIfBranches.cpp

Added: clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractFunction.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractFunction.cpp?rev=370249&view=auto
==
--- clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractFunction.cpp (added)
+++ clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractFunction.cpp Wed Aug 
28 12:34:17 2019
@@ -0,0 +1,605 @@
+//===--- ExtractFunction.cpp -*- 
C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// Extracts statements to a new function and replaces the statements with a
+// call to the new function.
+// Before:
+//   void f(int a) {
+// [[if(a < 5)
+//   a = 5;]]
+//   }
+// After:
+//   void extracted(int &a) {
+// if(a < 5)
+//   a = 5;
+//   }
+//   void f(int a) {
+// extracted(a);
+//   }
+//
+// - Only extract statements
+// - Extracts from non-templated free functions only.
+// - Parameters are const only if the declaration was const
+//   - Always passed by l-value reference
+// - Void return type
+// - Cannot extract declarations that will be needed in the original function
+//   after extraction.
+// - Doesn't check for broken control flow (break/continue without loop/switch)
+//
+// 1. ExtractFunction is the tweak subclass
+//- Prepare does basic analysis of the selection and is therefore fast.
+//  Successful prepare doesn't always mean we can apply the tweak.
+//- Apply does a more detailed analysis and can be slower. In case of
+//  failure, we let the user know that we are unable to perform extraction.
+// 2. ExtractionZone store information about the range being extracted and the
+//enclosing function.
+// 3. NewFunction stores properties of the extracted function and provides
+//methods for rendering it.
+// 4. CapturedZoneInfo uses a RecursiveASTVisitor to capture information about
+//the extraction like declarations, existing return statements, etc.
+// 5. getExtractedFunction is responsible for analyzing the CapturedZoneInfo 
and
+//creating a NewFunction.
+//===--===//
+
+#include "AST.h"
+#include "ClangdUnit.h"
+#include "Logger.h"
+#include "Selection.h"
+#include "SourceCode.h"
+#include "refactor/Tweak.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/Decl.h"
+#include "clang/AST/DeclTemplate.h"
+#include "clang/AST/RecursiveASTVisitor.h"
+#include "clang/AST/Stmt.h"
+#include "clang/Basic/LangOptions.h"
+#include "clang/Basic/SourceLocation.h"
+#include "clang/Basic/SourceManager.h"
+#include "clang/Lex/Lexer.h"
+#include "clang/Tooling/Core/Replacement.h"
+#include "clang/Tooling/Refactoring/Extract/SourceExtraction.h"
+#include "llvm/ADT/None.h"
+#include "llvm/ADT/Optional.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/iterator_range.h"
+#include "llvm/Support/Casting.h"
+#include "llvm/Support/Error.h"
+
+namespace clang {
+namespace clangd {
+namespace {
+
+using Node = SelectionTree::Node;
+
+// ExtractionZone is the part of code that is being extracted.
+// EnclosingFunction is the function/method inside which the zone 

[clang-tools-extra] r370372 - [Clangd] NFC: Added fixme for checking for local/anonymous types for extracted parameters

2019-08-29 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Thu Aug 29 08:11:59 2019
New Revision: 370372

URL: http://llvm.org/viewvc/llvm-project?rev=370372&view=rev
Log:
[Clangd] NFC: Added fixme for checking for local/anonymous types for extracted 
parameters

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

Modified: clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractFunction.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractFunction.cpp?rev=370372&r1=370371&r2=370372&view=diff
==
--- clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractFunction.cpp 
(original)
+++ clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractFunction.cpp Thu Aug 
29 08:11:59 2019
@@ -459,6 +459,7 @@ CapturedZoneInfo captureZoneInfo(const E
 // Adds parameters to ExtractedFunc.
 // Returns true if able to find the parameters successfully and no hoisting
 // needed.
+// FIXME: Check if the declaration has a local/anonymous type
 bool createParameters(NewFunction &ExtractedFunc,
   const CapturedZoneInfo &CapturedInfo) {
   for (const auto &KeyVal : CapturedInfo.DeclInfoMap) {


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


[clang-tools-extra] r370455 - [Clangd] ExtractFunction Added checks for broken control flow

2019-08-30 Thread Shaurya Gupta via cfe-commits
Author: sureyeaah
Date: Fri Aug 30 02:57:56 2019
New Revision: 370455

URL: http://llvm.org/viewvc/llvm-project?rev=370455&view=rev
Log:
[Clangd] ExtractFunction Added checks for broken control flow

Summary:
- Added checks for broken control flow
- Added unittests

Reviewers: sammccall, kadircet

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

Tags: #clang

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

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

Modified: clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractFunction.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractFunction.cpp?rev=370455&r1=370454&r2=370455&view=diff
==
--- clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractFunction.cpp 
(original)
+++ clang-tools-extra/trunk/clangd/refactor/tweaks/ExtractFunction.cpp Fri Aug 
30 02:57:56 2019
@@ -29,7 +29,7 @@
 // - Void return type
 // - Cannot extract declarations that will be needed in the original function
 //   after extraction.
-// - Doesn't check for broken control flow (break/continue without loop/switch)
+// - Checks for broken control flow (break/continue without loop/switch)
 //
 // 1. ExtractFunction is the tweak subclass
 //- Prepare does basic analysis of the selection and is therefore fast.
@@ -153,6 +153,7 @@ struct ExtractionZone {
   // semicolon after the extraction.
   const Node *getLastRootStmt() const { return Parent->Children.back(); }
   void generateRootStmts();
+
 private:
   llvm::DenseSet RootStmts;
 };
@@ -163,7 +164,7 @@ bool ExtractionZone::isRootStmt(const St
 
 // Generate RootStmts set
 void ExtractionZone::generateRootStmts() {
-  for(const Node *Child : Parent->Children)
+  for (const Node *Child : Parent->Children)
 RootStmts.insert(Child->ASTNode.get());
 }
 
@@ -179,7 +180,7 @@ const FunctionDecl *findEnclosingFunctio
   if (isa(Func))
 return nullptr;
   // FIXME: Support extraction from templated functions.
-  if(Func->isTemplated())
+  if (Func->isTemplated())
 return nullptr;
   return Func;
 }
@@ -351,8 +352,9 @@ struct CapturedZoneInfo {
   llvm::DenseMap DeclInfoMap;
   // True if there is a return statement in zone.
   bool HasReturnStmt = false;
-  // For now we just care whether there exists a break/continue in zone.
-  bool HasBreakOrContinue = false;
+  // Control flow is broken if we are extracting a break/continue without a
+  // corresponding parent loop/switch
+  bool BrokenControlFlow = false;
   // FIXME: capture TypeAliasDecl and UsingDirectiveDecl
   // FIXME: Capture type information as well.
   DeclInformation *createDeclInfo(const Decl *D, ZoneRelative RelativeLoc);
@@ -391,6 +393,11 @@ void CapturedZoneInfo::DeclInformation::
   }
 }
 
+bool isLoop(const Stmt *S) {
+  return isa(S) || isa(S) || isa(S) ||
+ isa(S);
+}
+
 // Captures information from Extraction Zone
 CapturedZoneInfo captureZoneInfo(const ExtractionZone &ExtZone) {
   // We use the ASTVisitor instead of using the selection tree since we need to
@@ -402,24 +409,53 @@ CapturedZoneInfo captureZoneInfo(const E
 ExtractionZoneVisitor(const ExtractionZone &ExtZone) : ExtZone(ExtZone) {
   TraverseDecl(const_cast(ExtZone.EnclosingFunction));
 }
+
 bool TraverseStmt(Stmt *S) {
+  if (!S)
+return true;
   bool IsRootStmt = ExtZone.isRootStmt(const_cast(S));
   // If we are starting traversal of a RootStmt, we are somewhere inside
   // ExtractionZone
   if (IsRootStmt)
 CurrentLocation = ZoneRelative::Inside;
+  addToLoopSwitchCounters(S, 1);
   // Traverse using base class's TraverseStmt
   RecursiveASTVisitor::TraverseStmt(S);
+  addToLoopSwitchCounters(S, -1);
   // We set the current location as after since next stmt will either be a
   // RootStmt (handled at the beginning) or after extractionZone
   if (IsRootStmt)
 CurrentLocation = ZoneRelative::After;
   return true;
 }
+
+// Add Increment to CurNumberOf{Loops,Switch} if statement is
+// {Loop,Switch} and inside Extraction Zone.
+void addToLoopSwitchCounters(Stmt *S, int Increment) {
+  if (CurrentLocation != ZoneRelative::Inside)
+return;
+  if (isLoop(S))
+CurNumberOfNestedLoops += Increment;
+  else if (isa(S))
+CurNumberOfSwitch += Increment;
+}
+
+// Decrement CurNumberOf{NestedLoops,Switch} if statement is {Loop,Switch}
+// and inside Extraction Zone.
+void decrementLoopSwitchCounters(Stmt *S) {
+  if (CurrentLocation != ZoneRelative::Inside)
+return;
+  if (isLoop(S))
+CurNumberOfNestedLoops--;
+  else if (isa(S))
+CurNumberOfSwitch--;
+}
+
 bool VisitDecl(Decl *D) {
   Info.createDeclInfo(D, CurrentLocati

[clang] 8bee4d4 - Revert "[LoopDeletion] Allows deletion of possibly infinite side-effect free loops"

2020-12-31 Thread Shaurya Gupta via cfe-commits

Author: Bogdan Graur
Date: 2020-12-31T11:47:49Z
New Revision: 8bee4d4e8f54b5f28b9117b552d3b2c655ff129b

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

LOG: Revert "[LoopDeletion] Allows deletion of possibly infinite side-effect 
free loops"

Test clang/test/Misc/loop-opt-setup.c fails when executed in Release.

This reverts commit 6f1503d59854b331f1f970d39839619b0a26bbc7.

Reviewed By: SureYeaah

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

Added: 


Modified: 
clang/test/Misc/loop-opt-setup.c
llvm/include/llvm/Transforms/Utils/LoopUtils.h
llvm/lib/Transforms/Scalar/LoopDeletion.cpp
llvm/lib/Transforms/Utils/LoopUtils.cpp
llvm/test/Transforms/LoopDeletion/no-exit-blocks.ll

Removed: 
llvm/test/Transforms/LoopDeletion/mustprogress.ll



diff  --git a/clang/test/Misc/loop-opt-setup.c 
b/clang/test/Misc/loop-opt-setup.c
index 660eea25c6af..c5c2ec4fda84 100644
--- a/clang/test/Misc/loop-opt-setup.c
+++ b/clang/test/Misc/loop-opt-setup.c
@@ -1,5 +1,4 @@
 // RUN: %clang -O1 -fno-unroll-loops -S -o - %s -emit-llvm | FileCheck %s
-// RUN: %clang -std=c99 -O1 -fno-unroll-loops -S -o - %s -emit-llvm | 
FileCheck %s --check-prefix C99
 
 extern int a[16];
 int b = 0;
@@ -25,12 +24,7 @@ void Helper() {
 }
 
 // Check br i1 to make sure the loop is gone, there will still be a label 
branch for the infinite loop.
-// In C99, there was no forward progress requirement, so we expect the 
infinite loop to still exist,
-// but for C11 and onwards, the infinite loop can be deleted.
 // CHECK-LABEL: Helper
-// C99: br label
-// C99-NOT: br i1
-// C99: br label
-// CHECK: entry:
-// CHECK-NOT: br i1
-// CHECK-NEXT: ret void
+// CHECK: br label
+// CHECK-NOT: br i1
+// CHECK: br label

diff  --git a/llvm/include/llvm/Transforms/Utils/LoopUtils.h 
b/llvm/include/llvm/Transforms/Utils/LoopUtils.h
index fc6b72eb28af..ba2bb0a4c6b0 100644
--- a/llvm/include/llvm/Transforms/Utils/LoopUtils.h
+++ b/llvm/include/llvm/Transforms/Utils/LoopUtils.h
@@ -255,9 +255,6 @@ bool hasDisableAllTransformsHint(const Loop *L);
 /// Look for the loop attribute that disables the LICM transformation 
heuristics.
 bool hasDisableLICMTransformsHint(const Loop *L);
 
-/// Look for the loop attribute that requires progress within the loop.
-bool hasMustProgress(const Loop *L);
-
 /// The mode sets how eager a transformation should be applied.
 enum TransformationMode {
   /// The pass can use heuristics to determine whether a transformation should

diff  --git a/llvm/lib/Transforms/Scalar/LoopDeletion.cpp 
b/llvm/lib/Transforms/Scalar/LoopDeletion.cpp
index 814cfc7ac6a9..065db647561e 100644
--- a/llvm/lib/Transforms/Scalar/LoopDeletion.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopDeletion.cpp
@@ -128,11 +128,10 @@ static bool isLoopNeverExecuted(Loop *L) {
 
 /// Remove a loop if it is dead.
 ///
-/// A loop is considered dead either if it does not impact the observable
-/// behavior of the program other than finite running time, or if it is
-/// required to make progress by an attribute such as 'mustprogress' or
-/// 'llvm.loop.mustprogress' and does not make any. This may remove
-/// infinite loops that have been required to make progress.
+/// A loop is considered dead if it does not impact the observable behavior of
+/// the program other than finite running time. This never removes a loop that
+/// might be infinite (unless it is never executed), as doing so could change
+/// the halting/non-halting nature of a program.
 ///
 /// This entire process relies pretty heavily on LoopSimplify form and LCSSA in
 /// order to make various safety checks work.
@@ -208,13 +207,11 @@ static LoopDeletionResult deleteLoopIfDead(Loop *L, 
DominatorTree &DT,
: LoopDeletionResult::Unmodified;
   }
 
-  // Don't remove loops for which we can't solve the trip count unless the loop
-  // was required to make progress but has been determined to be dead.
+  // Don't remove loops for which we can't solve the trip count.
+  // They could be infinite, in which case we'd be changing program behavior.
   const SCEV *S = SE.getConstantMaxBackedgeTakenCount(L);
-  if (isa(S) &&
-  !L->getHeader()->getParent()->mustProgress() && !hasMustProgress(L)) {
-LLVM_DEBUG(dbgs() << "Could not compute SCEV MaxBackedgeTakenCount and was 
"
- "not required to make progress.\n");
+  if (isa(S)) {
+LLVM_DEBUG(dbgs() << "Could not compute SCEV MaxBackedgeTakenCount.\n");
 return Changed ? LoopDeletionResult::Modified
: LoopDeletionResult::Unmodified;
   }

diff  --git a/llvm/lib/Transforms/Utils/LoopUtils.cpp 
b/llvm/lib/Transforms/Utils/LoopUtils.cpp
index 4cd59af6e551..8dc7709c6e55 100644
--- a/llvm/lib/Transforms/Utils/LoopUtils.cpp
+++ b/llv