r315924 - [refactor] allow the use of refactoring diagnostics

Alex Lorenz via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 16 11:28:26 PDT 2017


Author: arphaman
Date: Mon Oct 16 11:28:26 2017
New Revision: 315924

URL: http://llvm.org/viewvc/llvm-project?rev=315924&view=rev
Log:
[refactor] allow the use of refactoring diagnostics

This commit allows the refactoring library to use its own set of
refactoring-specific diagnostics to reports things like initiation errors.

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

Added:
    cfe/trunk/include/clang/Basic/DiagnosticRefactoringKinds.td
    cfe/trunk/include/clang/Tooling/Refactoring/RefactoringDiagnostic.h
    cfe/trunk/test/Refactor/LocalRename/NoSymbolSelectedError.cpp
    cfe/trunk/tools/clang-refactor/ToolRefactoringResultConsumer.h
Modified:
    cfe/trunk/include/clang/Basic/AllDiagnostics.h
    cfe/trunk/include/clang/Basic/CMakeLists.txt
    cfe/trunk/include/clang/Basic/Diagnostic.td
    cfe/trunk/include/clang/Basic/DiagnosticIDs.h
    cfe/trunk/include/clang/Tooling/Refactoring/RefactoringActionRuleRequirements.h
    cfe/trunk/include/clang/Tooling/Refactoring/RefactoringRuleContext.h
    cfe/trunk/include/clang/module.modulemap
    cfe/trunk/lib/Basic/DiagnosticIDs.cpp
    cfe/trunk/lib/Tooling/Refactoring/Rename/RenamingAction.cpp
    cfe/trunk/tools/clang-refactor/ClangRefactor.cpp
    cfe/trunk/tools/clang-refactor/TestSupport.cpp
    cfe/trunk/tools/clang-refactor/TestSupport.h
    cfe/trunk/tools/diagtool/DiagnosticNames.cpp
    cfe/trunk/unittests/Tooling/RefactoringActionRulesTest.cpp

Modified: cfe/trunk/include/clang/Basic/AllDiagnostics.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/AllDiagnostics.h?rev=315924&r1=315923&r2=315924&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/AllDiagnostics.h (original)
+++ cfe/trunk/include/clang/Basic/AllDiagnostics.h Mon Oct 16 11:28:26 2017
@@ -25,6 +25,7 @@
 #include "clang/Parse/ParseDiagnostic.h"
 #include "clang/Sema/SemaDiagnostic.h"
 #include "clang/Serialization/SerializationDiagnostic.h"
+#include "clang/Tooling/Refactoring/RefactoringDiagnostic.h"
 
 namespace clang {
 template <size_t SizeOfStr, typename FieldType>

Modified: cfe/trunk/include/clang/Basic/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/CMakeLists.txt?rev=315924&r1=315923&r2=315924&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/CMakeLists.txt (original)
+++ cfe/trunk/include/clang/Basic/CMakeLists.txt Mon Oct 16 11:28:26 2017
@@ -14,6 +14,7 @@ clang_diag_gen(Driver)
 clang_diag_gen(Frontend)
 clang_diag_gen(Lex)
 clang_diag_gen(Parse)
+clang_diag_gen(Refactoring)
 clang_diag_gen(Sema)
 clang_diag_gen(Serialization)
 clang_tablegen(DiagnosticGroups.inc -gen-clang-diag-groups

Modified: cfe/trunk/include/clang/Basic/Diagnostic.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Diagnostic.td?rev=315924&r1=315923&r2=315924&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/Diagnostic.td (original)
+++ cfe/trunk/include/clang/Basic/Diagnostic.td Mon Oct 16 11:28:26 2017
@@ -138,6 +138,7 @@ include "DiagnosticDriverKinds.td"
 include "DiagnosticFrontendKinds.td"
 include "DiagnosticLexKinds.td"
 include "DiagnosticParseKinds.td"
+include "DiagnosticRefactoringKinds.td"
 include "DiagnosticSemaKinds.td"
 include "DiagnosticSerializationKinds.td"
 

Modified: cfe/trunk/include/clang/Basic/DiagnosticIDs.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticIDs.h?rev=315924&r1=315923&r2=315924&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticIDs.h (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticIDs.h Mon Oct 16 11:28:26 2017
@@ -38,7 +38,8 @@ namespace clang {
       DIAG_SIZE_COMMENT       =  100,
       DIAG_SIZE_CROSSTU       =  100,
       DIAG_SIZE_SEMA          = 3500,
-      DIAG_SIZE_ANALYSIS      =  100
+      DIAG_SIZE_ANALYSIS      =  100,
+      DIAG_SIZE_REFACTORING   = 1000,
     };
     // Start position for diagnostics.
     enum {
@@ -53,7 +54,8 @@ namespace clang {
       DIAG_START_CROSSTU       = DIAG_START_COMMENT       + DIAG_SIZE_CROSSTU,
       DIAG_START_SEMA          = DIAG_START_CROSSTU       + DIAG_SIZE_COMMENT,
       DIAG_START_ANALYSIS      = DIAG_START_SEMA          + DIAG_SIZE_SEMA,
-      DIAG_UPPER_LIMIT         = DIAG_START_ANALYSIS      + DIAG_SIZE_ANALYSIS
+      DIAG_START_REFACTORING   = DIAG_START_ANALYSIS      + DIAG_SIZE_ANALYSIS,
+      DIAG_UPPER_LIMIT         = DIAG_START_REFACTORING   + DIAG_SIZE_REFACTORING
     };
 
     class CustomDiagInfo;

Added: cfe/trunk/include/clang/Basic/DiagnosticRefactoringKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticRefactoringKinds.td?rev=315924&view=auto
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticRefactoringKinds.td (added)
+++ cfe/trunk/include/clang/Basic/DiagnosticRefactoringKinds.td Mon Oct 16 11:28:26 2017
@@ -0,0 +1,25 @@
+//==--- DiagnosticRefactoringKinds.td - refactoring diagnostics -----------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+//===----------------------------------------------------------------------===//
+// Refactoring Diagnostics
+//===----------------------------------------------------------------------===//
+
+let Component = "Refactoring" in {
+
+let CategoryName = "Refactoring Invocation Issue" in {
+
+def err_refactor_no_selection : Error<"refactoring action can't be initiated "
+  "without a selection">;
+def err_refactor_selection_no_symbol : Error<"there is no symbol at the given "
+  "location">;
+
+}
+
+} // end of Refactoring diagnostics

Modified: cfe/trunk/include/clang/Tooling/Refactoring/RefactoringActionRuleRequirements.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/Refactoring/RefactoringActionRuleRequirements.h?rev=315924&r1=315923&r2=315924&view=diff
==============================================================================
--- cfe/trunk/include/clang/Tooling/Refactoring/RefactoringActionRuleRequirements.h (original)
+++ cfe/trunk/include/clang/Tooling/Refactoring/RefactoringActionRuleRequirements.h Mon Oct 16 11:28:26 2017
@@ -11,6 +11,7 @@
 #define LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_ACTION_RULE_REQUIREMENTS_H
 
 #include "clang/Basic/LLVM.h"
+#include "clang/Tooling/Refactoring/RefactoringDiagnostic.h"
 #include "clang/Tooling/Refactoring/RefactoringOption.h"
 #include "clang/Tooling/Refactoring/RefactoringRuleContext.h"
 #include "llvm/Support/Error.h"
@@ -47,10 +48,7 @@ public:
   Expected<SourceRange> evaluate(RefactoringRuleContext &Context) const {
     if (Context.getSelectionRange().isValid())
       return Context.getSelectionRange();
-    // FIXME: Use a diagnostic.
-    return llvm::make_error<llvm::StringError>(
-        "refactoring action can't be initiated without a selection",
-        llvm::inconvertibleErrorCode());
+    return Context.createDiagnosticError(diag::err_refactor_no_selection);
   }
 };
 

Added: cfe/trunk/include/clang/Tooling/Refactoring/RefactoringDiagnostic.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/Refactoring/RefactoringDiagnostic.h?rev=315924&view=auto
==============================================================================
--- cfe/trunk/include/clang/Tooling/Refactoring/RefactoringDiagnostic.h (added)
+++ cfe/trunk/include/clang/Tooling/Refactoring/RefactoringDiagnostic.h Mon Oct 16 11:28:26 2017
@@ -0,0 +1,30 @@
+//===--- RefactoringDiagnostic.h - ------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGDIAGNOSTIC_H
+#define LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGDIAGNOSTIC_H
+
+#include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/PartialDiagnostic.h"
+
+namespace clang {
+namespace diag {
+enum {
+#define DIAG(ENUM, FLAGS, DEFAULT_MAPPING, DESC, GROUP, SFINAE, NOWERROR,      \
+             SHOWINSYSHEADER, CATEGORY)                                        \
+  ENUM,
+#define REFACTORINGSTART
+#include "clang/Basic/DiagnosticRefactoringKinds.inc"
+#undef DIAG
+  NUM_BUILTIN_REFACTORING_DIAGNOSTICS
+};
+} // end namespace diag
+} // end namespace clang
+
+#endif // LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGDIAGNOSTIC_H

Modified: cfe/trunk/include/clang/Tooling/Refactoring/RefactoringRuleContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/Refactoring/RefactoringRuleContext.h?rev=315924&r1=315923&r2=315924&view=diff
==============================================================================
--- cfe/trunk/include/clang/Tooling/Refactoring/RefactoringRuleContext.h (original)
+++ cfe/trunk/include/clang/Tooling/Refactoring/RefactoringRuleContext.h Mon Oct 16 11:28:26 2017
@@ -10,6 +10,7 @@
 #ifndef LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_RULE_CONTEXT_H
 #define LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_RULE_CONTEXT_H
 
+#include "clang/Basic/DiagnosticError.h"
 #include "clang/Basic/SourceManager.h"
 
 namespace clang {
@@ -50,6 +51,17 @@ public:
 
   void setASTContext(ASTContext &Context) { AST = &Context; }
 
+  /// Creates an llvm::Error value that contains a diagnostic.
+  ///
+  /// The errors should not outlive the context.
+  llvm::Error createDiagnosticError(SourceLocation Loc, unsigned DiagID) {
+    return DiagnosticError::create(Loc, PartialDiagnostic(DiagID, DiagStorage));
+  }
+
+  llvm::Error createDiagnosticError(unsigned DiagID) {
+    return createDiagnosticError(SourceLocation(), DiagID);
+  }
+
 private:
   /// The source manager for the translation unit / file on which a refactoring
   /// action might operate on.
@@ -60,6 +72,8 @@ private:
   /// An optional AST for the translation unit on which a refactoring action
   /// might operate on.
   ASTContext *AST = nullptr;
+  /// The allocator for diagnostics.
+  PartialDiagnostic::StorageAllocator DiagStorage;
 };
 
 } // end namespace tooling

Modified: cfe/trunk/include/clang/module.modulemap
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/module.modulemap?rev=315924&r1=315923&r2=315924&view=diff
==============================================================================
--- cfe/trunk/include/clang/module.modulemap (original)
+++ cfe/trunk/include/clang/module.modulemap Mon Oct 16 11:28:26 2017
@@ -71,6 +71,7 @@ module Clang_Diagnostics {
   module Parse { header "Parse/ParseDiagnostic.h" export * }
   module Sema { header "Sema/SemaDiagnostic.h" export * }
   module Serialization { header "Serialization/SerializationDiagnostic.h" export * }
+  module Refactoring { header "Tooling/Refactoring/RefactoringDiagnostic.h" export * }
 }
 
 module Clang_Driver {

Modified: cfe/trunk/lib/Basic/DiagnosticIDs.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/DiagnosticIDs.cpp?rev=315924&r1=315923&r2=315924&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/DiagnosticIDs.cpp (original)
+++ cfe/trunk/lib/Basic/DiagnosticIDs.cpp Mon Oct 16 11:28:26 2017
@@ -43,7 +43,7 @@ struct StaticDiagInfoRec {
   unsigned SFINAE : 2;
   unsigned WarnNoWerror : 1;
   unsigned WarnShowInSystemHeader : 1;
-  unsigned Category : 5;
+  unsigned Category : 6;
 
   uint16_t OptionGroupIndex;
 
@@ -88,6 +88,7 @@ VALIDATE_DIAG_SIZE(AST)
 VALIDATE_DIAG_SIZE(COMMENT)
 VALIDATE_DIAG_SIZE(SEMA)
 VALIDATE_DIAG_SIZE(ANALYSIS)
+VALIDATE_DIAG_SIZE(REFACTORING)
 #undef VALIDATE_DIAG_SIZE
 #undef STRINGIFY_NAME
 
@@ -112,6 +113,7 @@ static const StaticDiagInfoRec StaticDia
 #include "clang/Basic/DiagnosticCrossTUKinds.inc"
 #include "clang/Basic/DiagnosticSemaKinds.inc"
 #include "clang/Basic/DiagnosticAnalysisKinds.inc"
+#include "clang/Basic/DiagnosticRefactoringKinds.inc"
 #undef DIAG
 };
 
@@ -150,6 +152,7 @@ CATEGORY(COMMENT, AST)
 CATEGORY(CROSSTU, COMMENT)
 CATEGORY(SEMA, CROSSTU)
 CATEGORY(ANALYSIS, SEMA)
+CATEGORY(REFACTORING, ANALYSIS)
 #undef CATEGORY
 
   // Avoid out of bounds reads.

Modified: cfe/trunk/lib/Tooling/Refactoring/Rename/RenamingAction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Refactoring/Rename/RenamingAction.cpp?rev=315924&r1=315923&r2=315924&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/Refactoring/Rename/RenamingAction.cpp (original)
+++ cfe/trunk/lib/Tooling/Refactoring/Rename/RenamingAction.cpp Mon Oct 16 11:28:26 2017
@@ -23,6 +23,7 @@
 #include "clang/Tooling/CommonOptionsParser.h"
 #include "clang/Tooling/Refactoring.h"
 #include "clang/Tooling/Refactoring/RefactoringAction.h"
+#include "clang/Tooling/Refactoring/RefactoringDiagnostic.h"
 #include "clang/Tooling/Refactoring/RefactoringOptions.h"
 #include "clang/Tooling/Refactoring/Rename/SymbolName.h"
 #include "clang/Tooling/Refactoring/Rename/USRFinder.h"
@@ -49,11 +50,9 @@ public:
       return Selection.takeError();
     const NamedDecl *ND =
         getNamedDeclAt(Context.getASTContext(), Selection->getBegin());
-    if (!ND) {
-      // FIXME: Use a diagnostic.
-      return llvm::make_error<StringError>("no symbol selected",
-                                           llvm::inconvertibleErrorCode());
-    }
+    if (!ND)
+      return Context.createDiagnosticError(
+          Selection->getBegin(), diag::err_refactor_selection_no_symbol);
     return getCanonicalSymbolDeclaration(ND);
   }
 };

Added: cfe/trunk/test/Refactor/LocalRename/NoSymbolSelectedError.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Refactor/LocalRename/NoSymbolSelectedError.cpp?rev=315924&view=auto
==============================================================================
--- cfe/trunk/test/Refactor/LocalRename/NoSymbolSelectedError.cpp (added)
+++ cfe/trunk/test/Refactor/LocalRename/NoSymbolSelectedError.cpp Mon Oct 16 11:28:26 2017
@@ -0,0 +1,8 @@
+// RUN: not clang-refactor local-rename -selection=%s:4:1 -new-name=Bar %s -- 2>&1 | FileCheck %s
+// RUN: clang-refactor local-rename -selection=test:%s -new-name=Bar %s -- 2>&1 | FileCheck --check-prefix=TESTCHECK %s
+
+class Baz { // CHECK: [[@LINE]]:1: error: there is no symbol at the given location
+};
+/*range=*/;
+// TESTCHECK:      1 '' results:
+// TESTCHECK-NEXT: there is no symbol at the given location

Modified: cfe/trunk/tools/clang-refactor/ClangRefactor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-refactor/ClangRefactor.cpp?rev=315924&r1=315923&r2=315924&view=diff
==============================================================================
--- cfe/trunk/tools/clang-refactor/ClangRefactor.cpp (original)
+++ cfe/trunk/tools/clang-refactor/ClangRefactor.cpp Mon Oct 16 11:28:26 2017
@@ -15,6 +15,7 @@
 
 #include "TestSupport.h"
 #include "clang/Frontend/CommandLineSourceLoc.h"
+#include "clang/Frontend/TextDiagnosticPrinter.h"
 #include "clang/Rewrite/Core/Rewriter.h"
 #include "clang/Tooling/CommonOptionsParser.h"
 #include "clang/Tooling/Refactoring.h"
@@ -65,7 +66,8 @@ public:
   /// logic into the refactoring operation. The test-specific consumer
   /// ensures that the individual results in a particular test group are
   /// identical.
-  virtual std::unique_ptr<RefactoringResultConsumer> createCustomConsumer() {
+  virtual std::unique_ptr<ClangRefactorToolConsumerInterface>
+  createCustomConsumer() {
     return nullptr;
   }
 
@@ -85,7 +87,8 @@ public:
 
   void print(raw_ostream &OS) override { TestSelections.dump(OS); }
 
-  std::unique_ptr<RefactoringResultConsumer> createCustomConsumer() override {
+  std::unique_ptr<ClangRefactorToolConsumerInterface>
+  createCustomConsumer() override {
     return TestSelections.createConsumer();
   }
 
@@ -304,10 +307,20 @@ private:
   RefactoringActionCommandLineOptions Options;
 };
 
-class ClangRefactorConsumer : public RefactoringResultConsumer {
+class ClangRefactorConsumer final : public ClangRefactorToolConsumerInterface {
 public:
+  ClangRefactorConsumer() {}
+
   void handleError(llvm::Error Err) override {
-    llvm::errs() << llvm::toString(std::move(Err)) << "\n";
+    Optional<PartialDiagnosticAt> Diag = DiagnosticError::take(Err);
+    if (!Diag) {
+      llvm::errs() << llvm::toString(std::move(Err)) << "\n";
+      return;
+    }
+    llvm::cantFail(std::move(Err)); // This is a success.
+    DiagnosticBuilder DB(
+        getDiags().Report(Diag->first, Diag->second.getDiagID()));
+    Diag->second.Emit(DB);
   }
 
   void handle(AtomicChanges Changes) override {
@@ -468,8 +481,8 @@ public:
       return true;
     }
 
-    bool HasFailed = false;
     ClangRefactorConsumer Consumer;
+    bool HasFailed = false;
     if (foreachTranslationUnit(DB, Sources, [&](ASTContext &AST) {
           RefactoringRuleContext Context(AST.getSourceManager());
           Context.setASTContext(AST);
@@ -488,21 +501,27 @@ public:
                 "The action must have at least one selection rule");
           };
 
+          std::unique_ptr<ClangRefactorToolConsumerInterface> CustomConsumer;
+          if (HasSelection)
+            CustomConsumer = Subcommand.getSelection()->createCustomConsumer();
+          ClangRefactorToolConsumerInterface &ActiveConsumer =
+              CustomConsumer ? *CustomConsumer : Consumer;
+          ActiveConsumer.beginTU(AST);
           if (HasSelection) {
             assert(Subcommand.getSelection() && "Missing selection argument?");
             if (opts::Verbose)
               Subcommand.getSelection()->print(llvm::outs());
-            auto CustomConsumer =
-                Subcommand.getSelection()->createCustomConsumer();
             if (Subcommand.getSelection()->forAllRanges(
                     Context.getSources(), [&](SourceRange R) {
                       Context.setSelectionRange(R);
-                      InvokeRule(CustomConsumer ? *CustomConsumer : Consumer);
+                      InvokeRule(ActiveConsumer);
                     }))
               HasFailed = true;
+            ActiveConsumer.endTU();
             return;
           }
           // FIXME (Alex L): Implement non-selection based invocation path.
+          ActiveConsumer.endTU();
         }))
       return true;
     return HasFailed || applySourceChanges(Consumer.getSourceChanges());

Modified: cfe/trunk/tools/clang-refactor/TestSupport.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-refactor/TestSupport.cpp?rev=315924&r1=315923&r2=315924&view=diff
==============================================================================
--- cfe/trunk/tools/clang-refactor/TestSupport.cpp (original)
+++ cfe/trunk/tools/clang-refactor/TestSupport.cpp Mon Oct 16 11:28:26 2017
@@ -14,6 +14,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "TestSupport.h"
+#include "clang/Basic/DiagnosticError.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Lex/Lexer.h"
 #include "llvm/ADT/STLExtras.h"
@@ -106,7 +107,7 @@ bool printRewrittenSources(const tooling
 }
 
 class TestRefactoringResultConsumer final
-    : public tooling::RefactoringResultConsumer {
+    : public ClangRefactorToolConsumerInterface {
 public:
   TestRefactoringResultConsumer(const TestSelectionRangesInFile &TestRanges)
       : TestRanges(TestRanges) {
@@ -182,10 +183,15 @@ bool TestRefactoringResultConsumer::hand
       std::string ErrorMessage;
       bool HasResult = !!Result;
       if (!HasResult) {
-        // FIXME: Handle diagnostic error as well.
-        handleAllErrors(Result.takeError(), [&](StringError &Err) {
-          ErrorMessage = Err.getMessage();
-        });
+        handleAllErrors(
+            Result.takeError(),
+            [&](StringError &Err) { ErrorMessage = Err.getMessage(); },
+            [&](DiagnosticError &Err) {
+              const PartialDiagnosticAt &Diag = Err.getDiagnostic();
+              llvm::SmallString<100> DiagText;
+              Diag.second.EmitToString(getDiags(), DiagText);
+              ErrorMessage = DiagText.str().str();
+            });
       }
       if (!CanonicalResult && !CanonicalErrorMessage) {
         if (HasResult)
@@ -248,7 +254,7 @@ bool TestRefactoringResultConsumer::hand
   return Failed;
 }
 
-std::unique_ptr<tooling::RefactoringResultConsumer>
+std::unique_ptr<ClangRefactorToolConsumerInterface>
 TestSelectionRangesInFile::createConsumer() const {
   return llvm::make_unique<TestRefactoringResultConsumer>(*this);
 }

Modified: cfe/trunk/tools/clang-refactor/TestSupport.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-refactor/TestSupport.h?rev=315924&r1=315923&r2=315924&view=diff
==============================================================================
--- cfe/trunk/tools/clang-refactor/TestSupport.h (original)
+++ cfe/trunk/tools/clang-refactor/TestSupport.h Mon Oct 16 11:28:26 2017
@@ -16,9 +16,9 @@
 #ifndef LLVM_CLANG_TOOLS_CLANG_REFACTOR_TEST_SUPPORT_H
 #define LLVM_CLANG_TOOLS_CLANG_REFACTOR_TEST_SUPPORT_H
 
+#include "ToolRefactoringResultConsumer.h"
 #include "clang/Basic/LLVM.h"
 #include "clang/Basic/SourceLocation.h"
-#include "clang/Tooling/Refactoring/RefactoringResultConsumer.h"
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/Support/Error.h"
@@ -65,7 +65,7 @@ struct TestSelectionRangesInFile {
   bool foreachRange(const SourceManager &SM,
                     llvm::function_ref<void(SourceRange)> Callback) const;
 
-  std::unique_ptr<tooling::RefactoringResultConsumer> createConsumer() const;
+  std::unique_ptr<ClangRefactorToolConsumerInterface> createConsumer() const;
 
   void dump(llvm::raw_ostream &OS) const;
 };

Added: cfe/trunk/tools/clang-refactor/ToolRefactoringResultConsumer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-refactor/ToolRefactoringResultConsumer.h?rev=315924&view=auto
==============================================================================
--- cfe/trunk/tools/clang-refactor/ToolRefactoringResultConsumer.h (added)
+++ cfe/trunk/tools/clang-refactor/ToolRefactoringResultConsumer.h Mon Oct 16 11:28:26 2017
@@ -0,0 +1,48 @@
+//===--- ToolRefactoringResultConsumer.h - ----------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_CLANG_REFACTOR_TOOL_REFACTORING_RESULT_CONSUMER_H
+#define LLVM_CLANG_TOOLS_CLANG_REFACTOR_TOOL_REFACTORING_RESULT_CONSUMER_H
+
+#include "clang/AST/ASTContext.h"
+#include "clang/Tooling/Refactoring/RefactoringResultConsumer.h"
+
+namespace clang {
+namespace refactor {
+
+/// An interface that subclasses the \c RefactoringResultConsumer interface
+/// that stores the reference to the TU-specific diagnostics engine.
+class ClangRefactorToolConsumerInterface
+    : public tooling::RefactoringResultConsumer {
+public:
+  /// Called when a TU is entered.
+  void beginTU(ASTContext &Context) {
+    assert(!Diags && "Diags has been set");
+    Diags = &Context.getDiagnostics();
+  }
+
+  /// Called when the tool is done with a TU.
+  void endTU() {
+    assert(Diags && "Diags unset");
+    Diags = nullptr;
+  }
+
+  DiagnosticsEngine &getDiags() const {
+    assert(Diags && "no diags");
+    return *Diags;
+  }
+
+private:
+  DiagnosticsEngine *Diags = nullptr;
+};
+
+} // end namespace refactor
+} // end namespace clang
+
+#endif // LLVM_CLANG_TOOLS_CLANG_REFACTOR_TOOL_REFACTORING_RESULT_CONSUMER_H

Modified: cfe/trunk/tools/diagtool/DiagnosticNames.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/diagtool/DiagnosticNames.cpp?rev=315924&r1=315923&r2=315924&view=diff
==============================================================================
--- cfe/trunk/tools/diagtool/DiagnosticNames.cpp (original)
+++ cfe/trunk/tools/diagtool/DiagnosticNames.cpp Mon Oct 16 11:28:26 2017
@@ -42,6 +42,7 @@ static const DiagnosticRecord BuiltinDia
 #include "clang/Basic/DiagnosticCommentKinds.inc"
 #include "clang/Basic/DiagnosticSemaKinds.inc"
 #include "clang/Basic/DiagnosticAnalysisKinds.inc"
+#include "clang/Basic/DiagnosticRefactoringKinds.inc"
 #undef DIAG
 };
 

Modified: cfe/trunk/unittests/Tooling/RefactoringActionRulesTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/RefactoringActionRulesTest.cpp?rev=315924&r1=315923&r2=315924&view=diff
==============================================================================
--- cfe/trunk/unittests/Tooling/RefactoringActionRulesTest.cpp (original)
+++ cfe/trunk/unittests/Tooling/RefactoringActionRulesTest.cpp Mon Oct 16 11:28:26 2017
@@ -11,6 +11,7 @@
 #include "RewriterTestContext.h"
 #include "clang/Tooling/Refactoring.h"
 #include "clang/Tooling/Refactoring/RefactoringActionRules.h"
+#include "clang/Tooling/Refactoring/RefactoringDiagnostic.h"
 #include "clang/Tooling/Refactoring/Rename/SymbolName.h"
 #include "clang/Tooling/Tooling.h"
 #include "llvm/Support/Errc.h"
@@ -128,12 +129,12 @@ TEST_F(RefactoringActionRulesTest, MyFir
         createReplacements(Rule, RefContext);
 
     ASSERT_TRUE(!ErrorOrResult);
-    std::string Message;
-    llvm::handleAllErrors(
-        ErrorOrResult.takeError(),
-        [&](llvm::StringError &Error) { Message = Error.getMessage(); });
-    EXPECT_EQ(Message,
-              "refactoring action can't be initiated without a selection");
+    unsigned DiagID;
+    llvm::handleAllErrors(ErrorOrResult.takeError(),
+                          [&](DiagnosticError &Error) {
+                            DiagID = Error.getDiagnostic().second.getDiagID();
+                          });
+    EXPECT_EQ(DiagID, diag::err_refactor_no_selection);
   }
 }
 




More information about the cfe-commits mailing list