[clang] Implement PrimitivesInit refactoring (PR #95867)

2024-06-17 Thread Pavel Desyatnikov via cfe-commits

https://github.com/desyatok created 
https://github.com/llvm/llvm-project/pull/95867

This is a gitlab mirror (LLVM repo is too large to directly create one)

>From 6ac71484e0974f6f928fcab43e02ced5f184ce3c Mon Sep 17 00:00:00 2001
From: Pavel Desyatnikov 
Date: Tue, 18 Jun 2024 02:41:55 +0300
Subject: [PATCH] Implement PrimitivesInit refactoring

---
 .../clang/Basic/DiagnosticRefactoringKinds.td |   6 +
 .../RefactoringActionRuleRequirements.h   |  17 ++
 .../Refactoring/VarInits/PrimitiveVarDecl.h   |  26 +++
 .../Refactoring/VarInits/PrimitivesInit.h |  40 +
 clang/lib/Tooling/Refactoring/CMakeLists.txt  |   3 +
 .../Refactoring/RefactoringActions.cpp|  18 +++
 .../Refactoring/VarInits/PrimitiveVarDecl.cpp |  55 +++
 .../VarInits/PrimitiveVarDeclRequirement.cpp  |  32 
 .../Refactoring/VarInits/PrimitivesInit.cpp   |  65 
 clang/tools/clang-refactor/ClangRefactor.cpp  | 149 +-
 10 files changed, 405 insertions(+), 6 deletions(-)
 create mode 100644 
clang/include/clang/Tooling/Refactoring/VarInits/PrimitiveVarDecl.h
 create mode 100644 
clang/include/clang/Tooling/Refactoring/VarInits/PrimitivesInit.h
 create mode 100644 clang/lib/Tooling/Refactoring/VarInits/PrimitiveVarDecl.cpp
 create mode 100644 
clang/lib/Tooling/Refactoring/VarInits/PrimitiveVarDeclRequirement.cpp
 create mode 100644 clang/lib/Tooling/Refactoring/VarInits/PrimitivesInit.cpp

diff --git a/clang/include/clang/Basic/DiagnosticRefactoringKinds.td 
b/clang/include/clang/Basic/DiagnosticRefactoringKinds.td
index 5446b32efbdd4..39366bd1492bb 100644
--- a/clang/include/clang/Basic/DiagnosticRefactoringKinds.td
+++ b/clang/include/clang/Basic/DiagnosticRefactoringKinds.td
@@ -28,6 +28,12 @@ def err_refactor_extract_simple_expression : Error<"the 
selected expression "
 def err_refactor_extract_prohibited_expression : Error<"the selected "
   "expression can't be extracted">;
 
+def err_refactor_no_vardecl : Error<"refactoring action can't be initiated "
+  "without a vardecl">;
+def err_refactor_initialized_variable : Error<"the provided vardecl is already 
initialized">;
+def err_refactor_global_variable_init : Error<"no need to initialize global 
variable">;
+def err_refactor_non_primitive_variable : Error<"refactoring action can't be 
initiated "
+  "with non-primitive variable">;
 }
 
 } // end of Refactoring diagnostics
diff --git 
a/clang/include/clang/Tooling/Refactoring/RefactoringActionRuleRequirements.h 
b/clang/include/clang/Tooling/Refactoring/RefactoringActionRuleRequirements.h
index 1a318da3acca1..bd410d43abd12 100644
--- 
a/clang/include/clang/Tooling/Refactoring/RefactoringActionRuleRequirements.h
+++ 
b/clang/include/clang/Tooling/Refactoring/RefactoringActionRuleRequirements.h
@@ -10,6 +10,7 @@
 #define LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGACTIONRULEREQUIREMENTS_H
 
 #include "clang/Basic/LLVM.h"
+#include "clang/Basic/SourceLocation.h"
 #include "clang/Tooling/Refactoring/ASTSelection.h"
 #include "clang/Tooling/Refactoring/RefactoringDiagnostic.h"
 #include "clang/Tooling/Refactoring/RefactoringOption.h"
@@ -77,6 +78,17 @@ class CodeRangeASTSelectionRequirement : public 
ASTSelectionRequirement {
   evaluate(RefactoringRuleContext &Context) const;
 };
 
+/// A base class for any requirement that expects source code position
+/// (or the refactoring tool with the -location option).
+class SourceLocationRequirement : public RefactoringActionRuleRequirement {
+public:
+  Expected evaluate(RefactoringRuleContext &Context) const {
+if (Context.getLocation().isValid())
+  return Context.getLocation();
+return Context.createDiagnosticError(diag::err_refactor_no_location);
+  }
+};
+
 /// A base class for any requirement that requires some refactoring options.
 class RefactoringOptionsRequirement : public RefactoringActionRuleRequirement {
 public:
@@ -116,6 +128,11 @@ class OptionRequirement : public 
RefactoringOptionsRequirement {
   std::shared_ptr Opt;
 };
 
+class PrimitiveVarDeclRequirement : public SourceLocationRequirement {
+public:
+  Expected evaluate(RefactoringRuleContext &Context) const;
+};
+
 } // end namespace tooling
 } // end namespace clang
 
diff --git 
a/clang/include/clang/Tooling/Refactoring/VarInits/PrimitiveVarDecl.h 
b/clang/include/clang/Tooling/Refactoring/VarInits/PrimitiveVarDecl.h
new file mode 100644
index 0..dd9ec755d30c7
--- /dev/null
+++ b/clang/include/clang/Tooling/Refactoring/VarInits/PrimitiveVarDecl.h
@@ -0,0 +1,26 @@
+//===--- PrimitiveVarDecl.h - 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_VARINITS_PRIMITIVEVARDECL_H
+#define LLVM_CLANG_TOOLING_REFACTORING_

[clang] Implement PrimitivesInit refactoring (PR #95867)

2024-06-17 Thread Pavel Desyatnikov via cfe-commits

https://github.com/desyatok closed 
https://github.com/llvm/llvm-project/pull/95867
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits