diff options
author | Clement Courbet <courbet@google.com> | 2025-01-22 13:42:00 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-01-22 13:42:00 +0100 |
commit | fbd86d05fe51d45f19df8d63aee41d979c268f8f (patch) | |
tree | 3fb1c0916b619344f7ff12c3ecaf16084842955f /clang/lib/Lex/Lexer.cpp | |
parent | c6c647588f911770170d2f7975b325f3d70cf89b (diff) | |
download | llvm-fbd86d05fe51d45f19df8d63aee41d979c268f8f.zip llvm-fbd86d05fe51d45f19df8d63aee41d979c268f8f.tar.gz llvm-fbd86d05fe51d45f19df8d63aee41d979c268f8f.tar.bz2 |
[clang-reorder-fields] Reorder leading comments (#123740)
Similarly to https://github.com/llvm/llvm-project/pull/122918, leading
comments are currently not being moved.
```
struct Foo {
// This one is the cool field.
int a;
int b;
};
```
becomes:
```
struct Foo {
// This one is the cool field.
int b;
int a;
};
```
but should be:
```
struct Foo {
int b;
// This one is the cool field.
int a;
};
```
Diffstat (limited to 'clang/lib/Lex/Lexer.cpp')
-rw-r--r-- | clang/lib/Lex/Lexer.cpp | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp index 115b6c1..087c6f1 100644 --- a/clang/lib/Lex/Lexer.cpp +++ b/clang/lib/Lex/Lexer.cpp @@ -1352,6 +1352,27 @@ std::optional<Token> Lexer::findNextToken(SourceLocation Loc, return Tok; } +std::optional<Token> Lexer::findPreviousToken(SourceLocation Loc, + const SourceManager &SM, + const LangOptions &LangOpts, + bool IncludeComments) { + const auto StartOfFile = SM.getLocForStartOfFile(SM.getFileID(Loc)); + while (Loc != StartOfFile) { + Loc = Loc.getLocWithOffset(-1); + if (Loc.isInvalid()) + return std::nullopt; + + Loc = GetBeginningOfToken(Loc, SM, LangOpts); + Token Tok; + if (getRawToken(Loc, Tok, SM, LangOpts)) + continue; // Not a token, go to prev location. + if (!Tok.is(tok::comment) || IncludeComments) { + return Tok; + } + } + return std::nullopt; +} + /// Checks that the given token is the first token that occurs after the /// given location (this excludes comments and whitespace). Returns the location /// immediately after the specified token. If the token is not found or the |