[clang] be31f2c - [clang-format][NFC] Move IndexedTokenSource to FormatTokenSource header.
Manuel Klimek via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 31 07:08:53 PST 2023
Author: Manuel Klimek
Date: 2023-01-31T15:06:20Z
New Revision: be31f2c11d47d3eca28d922a06de181d6e23b355
URL: https://github.com/llvm/llvm-project/commit/be31f2c11d47d3eca28d922a06de181d6e23b355
DIFF: https://github.com/llvm/llvm-project/commit/be31f2c11d47d3eca28d922a06de181d6e23b355.diff
LOG: [clang-format][NFC] Move IndexedTokenSource to FormatTokenSource header.
Finish refactoring of the token sources towards a single location.
Added:
Modified:
clang/lib/Format/FormatTokenSource.h
clang/lib/Format/UnwrappedLineParser.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/FormatTokenSource.h b/clang/lib/Format/FormatTokenSource.h
index 11c735d36864..e14f72b7d1f4 100644
--- a/clang/lib/Format/FormatTokenSource.h
+++ b/clang/lib/Format/FormatTokenSource.h
@@ -52,6 +52,71 @@ class FormatTokenSource {
virtual FormatToken *setPosition(unsigned Position) = 0;
};
+class IndexedTokenSource : public FormatTokenSource {
+public:
+ IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
+ : Tokens(Tokens), Position(-1) {}
+
+ FormatToken *getNextToken() override {
+ if (Position >= 0 && isEOF()) {
+ LLVM_DEBUG({
+ llvm::dbgs() << "Next ";
+ dbgToken(Position);
+ });
+ return Tokens[Position];
+ }
+ ++Position;
+ LLVM_DEBUG({
+ llvm::dbgs() << "Next ";
+ dbgToken(Position);
+ });
+ return Tokens[Position];
+ }
+
+ FormatToken *getPreviousToken() override {
+ return Position > 0 ? Tokens[Position - 1] : nullptr;
+ }
+
+ FormatToken *peekNextToken(bool SkipComment) override {
+ int Next = Position + 1;
+ if (SkipComment)
+ while (Tokens[Next]->is(tok::comment))
+ ++Next;
+ LLVM_DEBUG({
+ llvm::dbgs() << "Peeking ";
+ dbgToken(Next);
+ });
+ return Tokens[Next];
+ }
+
+ bool isEOF() override { return Tokens[Position]->is(tok::eof); }
+
+ unsigned getPosition() override {
+ LLVM_DEBUG(llvm::dbgs() << "Getting Position: " << Position << "\n");
+ assert(Position >= 0);
+ return Position;
+ }
+
+ FormatToken *setPosition(unsigned P) override {
+ LLVM_DEBUG(llvm::dbgs() << "Setting Position: " << P << "\n");
+ Position = P;
+ return Tokens[Position];
+ }
+
+ void reset() { Position = -1; }
+
+private:
+ void dbgToken(int Position, llvm::StringRef Indent = "") {
+ FormatToken *Tok = Tokens[Position];
+ llvm::dbgs() << Indent << "[" << Position
+ << "] Token: " << Tok->Tok.getName() << " / " << Tok->TokenText
+ << ", Macro: " << !!Tok->MacroCtx << "\n";
+ }
+
+ ArrayRef<FormatToken *> Tokens;
+ int Position;
+};
+
class ScopedMacroState : public FormatTokenSource {
public:
ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 9d90e5390bb8..c211c0ceb067 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -143,75 +143,6 @@ class CompoundStatementIndenter {
unsigned OldLineLevel;
};
-namespace {
-
-class IndexedTokenSource : public FormatTokenSource {
-public:
- IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
- : Tokens(Tokens), Position(-1) {}
-
- FormatToken *getNextToken() override {
- if (Position >= 0 && isEOF()) {
- LLVM_DEBUG({
- llvm::dbgs() << "Next ";
- dbgToken(Position);
- });
- return Tokens[Position];
- }
- ++Position;
- LLVM_DEBUG({
- llvm::dbgs() << "Next ";
- dbgToken(Position);
- });
- return Tokens[Position];
- }
-
- FormatToken *getPreviousToken() override {
- return Position > 0 ? Tokens[Position - 1] : nullptr;
- }
-
- FormatToken *peekNextToken(bool SkipComment) override {
- int Next = Position + 1;
- if (SkipComment)
- while (Tokens[Next]->is(tok::comment))
- ++Next;
- LLVM_DEBUG({
- llvm::dbgs() << "Peeking ";
- dbgToken(Next);
- });
- return Tokens[Next];
- }
-
- bool isEOF() override { return Tokens[Position]->is(tok::eof); }
-
- unsigned getPosition() override {
- LLVM_DEBUG(llvm::dbgs() << "Getting Position: " << Position << "\n");
- assert(Position >= 0);
- return Position;
- }
-
- FormatToken *setPosition(unsigned P) override {
- LLVM_DEBUG(llvm::dbgs() << "Setting Position: " << P << "\n");
- Position = P;
- return Tokens[Position];
- }
-
- void reset() { Position = -1; }
-
-private:
- void dbgToken(int Position, llvm::StringRef Indent = "") {
- FormatToken *Tok = Tokens[Position];
- llvm::dbgs() << Indent << "[" << Position
- << "] Token: " << Tok->Tok.getName() << " / " << Tok->TokenText
- << ", Macro: " << !!Tok->MacroCtx << "\n";
- }
-
- ArrayRef<FormatToken *> Tokens;
- int Position;
-};
-
-} // end anonymous namespace
-
UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
const AdditionalKeywords &Keywords,
unsigned FirstStartColumn,
More information about the cfe-commits
mailing list