Skip to content

Commit add8920

Browse files
committed
Minor refractor
1 parent c66fcea commit add8920

File tree

2 files changed

+39
-39
lines changed

2 files changed

+39
-39
lines changed

mlir/lib/Tools/mlir-query/Matcher/Diagnostics.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,14 @@ static void printErrorContentToStream(const Diagnostics::ErrorContent &content,
187187
}
188188

189189
void Diagnostics::printToStream(llvm::raw_ostream &OS) const {
190-
for (size_t i = 0, e = errorValues.size(); i != e; ++i) {
191-
if (i != 0)
190+
for (const ErrorContent &error : errorValues) {
191+
if (&error != &errorValues.front())
192192
OS << "\n";
193-
printErrorContentToStream(errorValues[i], OS);
193+
for (const ContextFrame &frame : error.contextStack) {
194+
printContextFrameToStream(frame, OS);
195+
OS << "\n";
196+
}
197+
printErrorContentToStream(error, OS);
194198
}
195199
}
196200

@@ -202,12 +206,11 @@ std::string Diagnostics::toString() const {
202206
}
203207

204208
void Diagnostics::printToStreamFull(llvm::raw_ostream &OS) const {
205-
for (size_t i = 0, e = errorValues.size(); i != e; ++i) {
206-
if (i != 0)
209+
for (const ErrorContent &error : errorValues) {
210+
if (&error != &errorValues.front())
207211
OS << "\n";
208-
const ErrorContent &error = errorValues[i];
209-
for (size_t i = 0, e = error.contextStack.size(); i != e; ++i) {
210-
printContextFrameToStream(error.contextStack[i], OS);
212+
for (const ContextFrame &frame : error.contextStack) {
213+
printContextFrameToStream(frame, OS);
211214
OS << "\n";
212215
}
213216
printErrorContentToStream(error, OS);
@@ -223,4 +226,4 @@ std::string Diagnostics::toStringFull() const {
223226

224227
} // namespace matcher
225228
} // namespace query
226-
} // namespace mlir
229+
} // namespace mlir

mlir/lib/Tools/mlir-query/Matcher/Diagnostics.h

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,19 @@ namespace mlir {
2424
namespace query {
2525
namespace matcher {
2626

27+
// Represents the line and column numbers in a source file.
2728
struct SourceLocation {
28-
SourceLocation() : line(), column() {}
29-
unsigned line;
30-
unsigned column;
29+
unsigned line{};
30+
unsigned column{};
3131
};
3232

33+
// Represents a range in a source file, defined by its start and end locations.
3334
struct SourceRange {
34-
SourceLocation start;
35-
SourceLocation end;
35+
SourceLocation start{};
36+
SourceLocation end{};
3637
};
3738

38-
// Helper class to manage error messages.
39+
// Diagnostics class to manage error messages.
3940
class Diagnostics {
4041
public:
4142
// Parser context types.
@@ -62,7 +63,7 @@ class Diagnostics {
6263
ET_ParserFailedToBuildMatcher
6364
};
6465

65-
// Helper stream class.
66+
// Helper stream class for constructing error messages.
6667
class ArgStream {
6768
public:
6869
ArgStream(std::vector<std::string> *out) : out(out) {}
@@ -76,13 +77,8 @@ class Diagnostics {
7677
std::vector<std::string> *out;
7778
};
7879

79-
// Class defining a parser context.
80-
// Used by the parser to specify (possibly recursive) contexts where the
81-
// parsing/construction can fail. Any error triggered within a context will
82-
// keep information about the context chain.
83-
// This class should be used as a RAII instance in the stack.
80+
// Context for constructing a matcher or parsing its argument.
8481
struct Context {
85-
public:
8682
// About to call the constructor for a matcher.
8783
enum ConstructMatcherEnum { ConstructMatcher };
8884
Context(ConstructMatcherEnum, Diagnostics *error,
@@ -97,26 +93,22 @@ class Diagnostics {
9793
Diagnostics *const error;
9894
};
9995

100-
// Context for overloaded matcher construction.
101-
// This context will take care of merging all errors that happen within it
102-
// as "candidate" overloads for the same matcher.
96+
// Context for managing overloaded matcher construction.
10397
struct OverloadContext {
104-
public:
98+
// Construct an overload context with the given error.
10599
OverloadContext(Diagnostics *error);
106100
~OverloadContext();
107-
108-
// Revert all errors that happened within this context.
101+
// Revert all errors that occurred within this context.
109102
void revertErrors();
110103

111104
private:
112105
Diagnostics *const error;
113-
unsigned beginIndex;
106+
unsigned beginIndex{};
114107
};
115108

116-
// Add an error to the diagnostics.
117-
// All the context information will be kept on the error message.
118-
// Returns a helper class to allow the caller to pass the arguments for the
119-
// error message, using the << operator.
109+
// Add an error message with the specified range and error type.
110+
// Returns an ArgStream object to allow constructing the error message using
111+
// the << operator.
120112
ArgStream addError(SourceRange range, ErrorType error);
121113

122114
// Information stored for one frame of the context.
@@ -136,20 +128,25 @@ class Diagnostics {
136128
};
137129
std::vector<Message> messages;
138130
};
131+
132+
// Get an array reference to the error contents.
139133
llvm::ArrayRef<ErrorContent> errors() const { return errorValues; }
140134

141-
// Returns a simple string representation of each error.
142-
// Each error only shows the error message without any context.
135+
// Print all error messages to the specified output stream.
143136
void printToStream(llvm::raw_ostream &OS) const;
137+
// Get a string representation of all error messages.
144138
std::string toString() const;
145139

146-
// Returns the full string representation of each error.
147-
// Each error message contains the full context.
140+
// Print the full error messages, including the context information, to the
141+
// specified output stream.
148142
void printToStreamFull(llvm::raw_ostream &OS) const;
143+
// Get the full string representation of all error messages, including the
144+
// context information.
149145
std::string toStringFull() const;
150146

151147
private:
152-
// Helper function used by the constructors of ContextFrame.
148+
// Push a new context frame onto the context stack with the specified type and
149+
// range.
153150
ArgStream pushContextFrame(ContextType type, SourceRange range);
154151

155152
std::vector<ContextFrame> contextStack;
@@ -160,4 +157,4 @@ class Diagnostics {
160157
} // namespace query
161158
} // namespace mlir
162159

163-
#endif // MLIR_TOOLS_MLIRQUERY_MATCHERDIAGNOSTICS_H
160+
#endif // MLIR_TOOLS_MLIRQUERY_MATCHERDIAGNOSTICS_H

0 commit comments

Comments
 (0)