Skip to content

Commit f5ca27c

Browse files
committed
[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 llvm-svn: 315924
1 parent 5284350 commit f5ca27c

File tree

18 files changed

+192
-34
lines changed

18 files changed

+192
-34
lines changed

clang/include/clang/Basic/AllDiagnostics.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "clang/Parse/ParseDiagnostic.h"
2626
#include "clang/Sema/SemaDiagnostic.h"
2727
#include "clang/Serialization/SerializationDiagnostic.h"
28+
#include "clang/Tooling/Refactoring/RefactoringDiagnostic.h"
2829

2930
namespace clang {
3031
template <size_t SizeOfStr, typename FieldType>

clang/include/clang/Basic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ clang_diag_gen(Driver)
1414
clang_diag_gen(Frontend)
1515
clang_diag_gen(Lex)
1616
clang_diag_gen(Parse)
17+
clang_diag_gen(Refactoring)
1718
clang_diag_gen(Sema)
1819
clang_diag_gen(Serialization)
1920
clang_tablegen(DiagnosticGroups.inc -gen-clang-diag-groups

clang/include/clang/Basic/Diagnostic.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ include "DiagnosticDriverKinds.td"
138138
include "DiagnosticFrontendKinds.td"
139139
include "DiagnosticLexKinds.td"
140140
include "DiagnosticParseKinds.td"
141+
include "DiagnosticRefactoringKinds.td"
141142
include "DiagnosticSemaKinds.td"
142143
include "DiagnosticSerializationKinds.td"
143144

clang/include/clang/Basic/DiagnosticIDs.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ namespace clang {
3838
DIAG_SIZE_COMMENT = 100,
3939
DIAG_SIZE_CROSSTU = 100,
4040
DIAG_SIZE_SEMA = 3500,
41-
DIAG_SIZE_ANALYSIS = 100
41+
DIAG_SIZE_ANALYSIS = 100,
42+
DIAG_SIZE_REFACTORING = 1000,
4243
};
4344
// Start position for diagnostics.
4445
enum {
@@ -53,7 +54,8 @@ namespace clang {
5354
DIAG_START_CROSSTU = DIAG_START_COMMENT + DIAG_SIZE_CROSSTU,
5455
DIAG_START_SEMA = DIAG_START_CROSSTU + DIAG_SIZE_COMMENT,
5556
DIAG_START_ANALYSIS = DIAG_START_SEMA + DIAG_SIZE_SEMA,
56-
DIAG_UPPER_LIMIT = DIAG_START_ANALYSIS + DIAG_SIZE_ANALYSIS
57+
DIAG_START_REFACTORING = DIAG_START_ANALYSIS + DIAG_SIZE_ANALYSIS,
58+
DIAG_UPPER_LIMIT = DIAG_START_REFACTORING + DIAG_SIZE_REFACTORING
5759
};
5860

5961
class CustomDiagInfo;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//==--- DiagnosticRefactoringKinds.td - refactoring diagnostics -----------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
//===----------------------------------------------------------------------===//
11+
// Refactoring Diagnostics
12+
//===----------------------------------------------------------------------===//
13+
14+
let Component = "Refactoring" in {
15+
16+
let CategoryName = "Refactoring Invocation Issue" in {
17+
18+
def err_refactor_no_selection : Error<"refactoring action can't be initiated "
19+
"without a selection">;
20+
def err_refactor_selection_no_symbol : Error<"there is no symbol at the given "
21+
"location">;
22+
23+
}
24+
25+
} // end of Refactoring diagnostics

clang/include/clang/Tooling/Refactoring/RefactoringActionRuleRequirements.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_ACTION_RULE_REQUIREMENTS_H
1212

1313
#include "clang/Basic/LLVM.h"
14+
#include "clang/Tooling/Refactoring/RefactoringDiagnostic.h"
1415
#include "clang/Tooling/Refactoring/RefactoringOption.h"
1516
#include "clang/Tooling/Refactoring/RefactoringRuleContext.h"
1617
#include "llvm/Support/Error.h"
@@ -47,10 +48,7 @@ class SourceRangeSelectionRequirement : public SourceSelectionRequirement {
4748
Expected<SourceRange> evaluate(RefactoringRuleContext &Context) const {
4849
if (Context.getSelectionRange().isValid())
4950
return Context.getSelectionRange();
50-
// FIXME: Use a diagnostic.
51-
return llvm::make_error<llvm::StringError>(
52-
"refactoring action can't be initiated without a selection",
53-
llvm::inconvertibleErrorCode());
51+
return Context.createDiagnosticError(diag::err_refactor_no_selection);
5452
}
5553
};
5654

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===--- RefactoringDiagnostic.h - ------------------------------*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGDIAGNOSTIC_H
11+
#define LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGDIAGNOSTIC_H
12+
13+
#include "clang/Basic/Diagnostic.h"
14+
#include "clang/Basic/PartialDiagnostic.h"
15+
16+
namespace clang {
17+
namespace diag {
18+
enum {
19+
#define DIAG(ENUM, FLAGS, DEFAULT_MAPPING, DESC, GROUP, SFINAE, NOWERROR, \
20+
SHOWINSYSHEADER, CATEGORY) \
21+
ENUM,
22+
#define REFACTORINGSTART
23+
#include "clang/Basic/DiagnosticRefactoringKinds.inc"
24+
#undef DIAG
25+
NUM_BUILTIN_REFACTORING_DIAGNOSTICS
26+
};
27+
} // end namespace diag
28+
} // end namespace clang
29+
30+
#endif // LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGDIAGNOSTIC_H

clang/include/clang/Tooling/Refactoring/RefactoringRuleContext.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#ifndef LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_RULE_CONTEXT_H
1111
#define LLVM_CLANG_TOOLING_REFACTOR_REFACTORING_RULE_CONTEXT_H
1212

13+
#include "clang/Basic/DiagnosticError.h"
1314
#include "clang/Basic/SourceManager.h"
1415

1516
namespace clang {
@@ -50,6 +51,17 @@ class RefactoringRuleContext {
5051

5152
void setASTContext(ASTContext &Context) { AST = &Context; }
5253

54+
/// Creates an llvm::Error value that contains a diagnostic.
55+
///
56+
/// The errors should not outlive the context.
57+
llvm::Error createDiagnosticError(SourceLocation Loc, unsigned DiagID) {
58+
return DiagnosticError::create(Loc, PartialDiagnostic(DiagID, DiagStorage));
59+
}
60+
61+
llvm::Error createDiagnosticError(unsigned DiagID) {
62+
return createDiagnosticError(SourceLocation(), DiagID);
63+
}
64+
5365
private:
5466
/// The source manager for the translation unit / file on which a refactoring
5567
/// action might operate on.
@@ -60,6 +72,8 @@ class RefactoringRuleContext {
6072
/// An optional AST for the translation unit on which a refactoring action
6173
/// might operate on.
6274
ASTContext *AST = nullptr;
75+
/// The allocator for diagnostics.
76+
PartialDiagnostic::StorageAllocator DiagStorage;
6377
};
6478

6579
} // end namespace tooling

clang/include/clang/module.modulemap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ module Clang_Diagnostics {
7171
module Parse { header "Parse/ParseDiagnostic.h" export * }
7272
module Sema { header "Sema/SemaDiagnostic.h" export * }
7373
module Serialization { header "Serialization/SerializationDiagnostic.h" export * }
74+
module Refactoring { header "Tooling/Refactoring/RefactoringDiagnostic.h" export * }
7475
}
7576

7677
module Clang_Driver {

clang/lib/Basic/DiagnosticIDs.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct StaticDiagInfoRec {
4343
unsigned SFINAE : 2;
4444
unsigned WarnNoWerror : 1;
4545
unsigned WarnShowInSystemHeader : 1;
46-
unsigned Category : 5;
46+
unsigned Category : 6;
4747

4848
uint16_t OptionGroupIndex;
4949

@@ -88,6 +88,7 @@ VALIDATE_DIAG_SIZE(AST)
8888
VALIDATE_DIAG_SIZE(COMMENT)
8989
VALIDATE_DIAG_SIZE(SEMA)
9090
VALIDATE_DIAG_SIZE(ANALYSIS)
91+
VALIDATE_DIAG_SIZE(REFACTORING)
9192
#undef VALIDATE_DIAG_SIZE
9293
#undef STRINGIFY_NAME
9394

@@ -112,6 +113,7 @@ static const StaticDiagInfoRec StaticDiagInfo[] = {
112113
#include "clang/Basic/DiagnosticCrossTUKinds.inc"
113114
#include "clang/Basic/DiagnosticSemaKinds.inc"
114115
#include "clang/Basic/DiagnosticAnalysisKinds.inc"
116+
#include "clang/Basic/DiagnosticRefactoringKinds.inc"
115117
#undef DIAG
116118
};
117119

@@ -150,6 +152,7 @@ CATEGORY(COMMENT, AST)
150152
CATEGORY(CROSSTU, COMMENT)
151153
CATEGORY(SEMA, CROSSTU)
152154
CATEGORY(ANALYSIS, SEMA)
155+
CATEGORY(REFACTORING, ANALYSIS)
153156
#undef CATEGORY
154157

155158
// Avoid out of bounds reads.

0 commit comments

Comments
 (0)