Skip to content

Commit 44b83e8

Browse files
authored
[clang-format] Add TT_AfterPPDirective for better annotation (#121622)
For now, we only need to annotate the token after #error or #warning. Fixes #117706.
1 parent 04610b9 commit 44b83e8

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

clang/lib/Format/FormatToken.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ namespace clang {
2525
namespace format {
2626

2727
#define LIST_TOKEN_TYPES \
28+
TYPE(AfterPPDirective) \
2829
TYPE(ArrayInitializerLSquare) \
2930
TYPE(ArraySubscriptLSquare) \
3031
TYPE(AttributeColon) \

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4941,6 +4941,10 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
49414941
Right.is(TT_ModulePartitionColon)) {
49424942
return true;
49434943
}
4944+
4945+
if (Right.is(TT_AfterPPDirective))
4946+
return true;
4947+
49444948
// No space between import foo:bar but keep a space between import :bar;
49454949
if (Left.is(tok::identifier) && Right.is(TT_ModulePartitionColon))
49464950
return false;

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,12 @@ void UnwrappedLineParser::parsePPDirective() {
10301030
case tok::pp_pragma:
10311031
parsePPPragma();
10321032
break;
1033+
case tok::pp_error:
1034+
case tok::pp_warning:
1035+
nextToken();
1036+
if (!eof() && Style.isCpp())
1037+
FormatTok->setFinalizedType(TT_AfterPPDirective);
1038+
[[fallthrough]];
10331039
default:
10341040
parsePPUnknown();
10351041
break;
@@ -1209,9 +1215,8 @@ void UnwrappedLineParser::parsePPPragma() {
12091215
}
12101216

12111217
void UnwrappedLineParser::parsePPUnknown() {
1212-
do {
1218+
while (!eof())
12131219
nextToken();
1214-
} while (!eof());
12151220
if (Style.IndentPPDirectives != FormatStyle::PPDIS_None)
12161221
Line->Level += PPBranchLevel + 1;
12171222
addUnwrappedLine();

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3635,6 +3635,13 @@ TEST_F(TokenAnnotatorTest, SwitchInMacroArgument) {
36353635
EXPECT_TOKEN(Tokens[9], tok::l_brace, TT_FunctionLBrace);
36363636
}
36373637

3638+
TEST_F(TokenAnnotatorTest, AfterPPDirective) {
3639+
auto Tokens = annotate("#error -- My error message");
3640+
3641+
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
3642+
EXPECT_TOKEN(Tokens[2], tok::minusminus, TT_AfterPPDirective);
3643+
}
3644+
36383645
} // namespace
36393646
} // namespace format
36403647
} // namespace clang

0 commit comments

Comments
 (0)