@@ -234,14 +234,15 @@ UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
234
234
CurrentLines (&Lines), Style(Style ), Keywords(Keywords),
235
235
CommentPragmasRegex(Style .CommentPragmas), Tokens(nullptr ),
236
236
Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1 ),
237
- IfNdefCondition(nullptr ), FoundIncludeGuardStart(false ),
238
- IncludeGuardRejected(false ), FirstStartColumn(FirstStartColumn) {}
237
+ IncludeGuard(Style .IndentPPDirectives == FormatStyle::PPDIS_None
238
+ ? IG_Rejected
239
+ : IG_Inited),
240
+ IncludeGuardToken(nullptr ), FirstStartColumn(FirstStartColumn) {}
239
241
240
242
void UnwrappedLineParser::reset () {
241
243
PPBranchLevel = -1 ;
242
- IfNdefCondition = nullptr ;
243
- FoundIncludeGuardStart = false ;
244
- IncludeGuardRejected = false ;
244
+ IncludeGuard = IG_Inited;
245
+ IncludeGuardToken = nullptr ;
245
246
Line.reset (new UnwrappedLine);
246
247
CommentsBeforeNextToken.clear ();
247
248
FormatTok = nullptr ;
@@ -264,6 +265,14 @@ void UnwrappedLineParser::parse() {
264
265
265
266
readToken ();
266
267
parseFile ();
268
+
269
+ // If we found an include guard then all preprocessor directives (other than
270
+ // the guard) are over-indented by one.
271
+ if (IncludeGuard == IG_Found)
272
+ for (auto &Line : Lines)
273
+ if (Line.InPPDirective && Line.Level > 0 )
274
+ --Line.Level ;
275
+
267
276
// Create line with eof token.
268
277
pushToken (FormatTok);
269
278
addUnwrappedLine ();
@@ -724,26 +733,28 @@ void UnwrappedLineParser::parsePPIf(bool IfDef) {
724
733
// If there's a #ifndef on the first line, and the only lines before it are
725
734
// comments, it could be an include guard.
726
735
bool MaybeIncludeGuard = IfNDef;
727
- if (!IncludeGuardRejected && !FoundIncludeGuardStart && MaybeIncludeGuard) {
736
+ if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
728
737
for (auto &Line : Lines) {
729
738
if (!Line.Tokens .front ().Tok ->is (tok::comment)) {
730
739
MaybeIncludeGuard = false ;
731
- IncludeGuardRejected = true ;
740
+ IncludeGuard = IG_Rejected ;
732
741
break ;
733
742
}
734
743
}
735
744
}
736
745
--PPBranchLevel;
737
746
parsePPUnknown ();
738
747
++PPBranchLevel;
739
- if (!IncludeGuardRejected && !FoundIncludeGuardStart && MaybeIncludeGuard)
740
- IfNdefCondition = IfCondition;
748
+ if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
749
+ IncludeGuard = IG_IfNdefed;
750
+ IncludeGuardToken = IfCondition;
751
+ }
741
752
}
742
753
743
754
void UnwrappedLineParser::parsePPElse () {
744
755
// If a potential include guard has an #else, it's not an include guard.
745
- if (FoundIncludeGuardStart && PPBranchLevel == 0 )
746
- FoundIncludeGuardStart = false ;
756
+ if (IncludeGuard == IG_Defined && PPBranchLevel == 0 )
757
+ IncludeGuard = IG_Rejected ;
747
758
conditionalCompilationAlternative ();
748
759
if (PPBranchLevel > -1 )
749
760
--PPBranchLevel;
@@ -757,34 +768,37 @@ void UnwrappedLineParser::parsePPEndIf() {
757
768
conditionalCompilationEnd ();
758
769
parsePPUnknown ();
759
770
// If the #endif of a potential include guard is the last thing in the file,
760
- // then we count it as a real include guard and subtract one from every
761
- // preprocessor indent.
771
+ // then we found an include guard.
762
772
unsigned TokenPosition = Tokens->getPosition ();
763
773
FormatToken *PeekNext = AllTokens[TokenPosition];
764
- if (FoundIncludeGuardStart && PPBranchLevel == -1 && PeekNext->is (tok::eof) &&
774
+ if (IncludeGuard == IG_Defined && PPBranchLevel == -1 &&
775
+ PeekNext->is (tok::eof) &&
765
776
Style .IndentPPDirectives != FormatStyle::PPDIS_None)
766
- for (auto &Line : Lines)
767
- if (Line.InPPDirective && Line.Level > 0 )
768
- --Line.Level ;
777
+ IncludeGuard = IG_Found;
769
778
}
770
779
771
780
void UnwrappedLineParser::parsePPDefine () {
772
781
nextToken ();
773
782
774
783
if (FormatTok->Tok .getKind () != tok::identifier) {
784
+ IncludeGuard = IG_Rejected;
785
+ IncludeGuardToken = nullptr ;
775
786
parsePPUnknown ();
776
787
return ;
777
788
}
778
- if (IfNdefCondition && IfNdefCondition->TokenText == FormatTok->TokenText ) {
779
- FoundIncludeGuardStart = true ;
789
+
790
+ if (IncludeGuard == IG_IfNdefed &&
791
+ IncludeGuardToken->TokenText == FormatTok->TokenText ) {
792
+ IncludeGuard = IG_Defined;
793
+ IncludeGuardToken = nullptr ;
780
794
for (auto &Line : Lines) {
781
795
if (!Line.Tokens .front ().Tok ->isOneOf (tok::comment, tok::hash)) {
782
- FoundIncludeGuardStart = false ;
796
+ IncludeGuard = IG_Rejected ;
783
797
break ;
784
798
}
785
799
}
786
800
}
787
- IfNdefCondition = nullptr ;
801
+
788
802
nextToken ();
789
803
if (FormatTok->Tok .getKind () == tok::l_paren &&
790
804
FormatTok->WhitespaceRange .getBegin () ==
@@ -811,7 +825,6 @@ void UnwrappedLineParser::parsePPUnknown() {
811
825
if (Style .IndentPPDirectives == FormatStyle::PPDIS_AfterHash)
812
826
Line->Level += PPBranchLevel + 1 ;
813
827
addUnwrappedLine ();
814
- IfNdefCondition = nullptr ;
815
828
}
816
829
817
830
// Here we blacklist certain tokens that are not usually the first token in an
0 commit comments