r329231 - [clang-format] In tests, expected code should be format-stable

2018-04-04 Thread Mark Zeren via cfe-commits
Author: mzeren-vmw
Date: Wed Apr  4 14:09:00 2018
New Revision: 329231

URL: http://llvm.org/viewvc/llvm-project?rev=329231&view=rev
Log:
[clang-format] In tests, expected code should be format-stable

Summary: Extend various verifyFormat helper functions to check that the
expected text is "stable". This provides some protection against bugs
where formatting results are ocilating between two forms, or continually
change in some other way.

Testing Done:

* Ran unit tests.

* Reproduced a known instability in preprocessor indentation which was
  caught by this new check.

Reviewers: krasimir

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D42034

Modified:
cfe/trunk/unittests/Format/FormatTest.cpp
cfe/trunk/unittests/Format/FormatTestComments.cpp
cfe/trunk/unittests/Format/FormatTestJS.cpp
cfe/trunk/unittests/Format/FormatTestJava.cpp
cfe/trunk/unittests/Format/FormatTestObjC.cpp
cfe/trunk/unittests/Format/FormatTestProto.cpp
cfe/trunk/unittests/Format/FormatTestTextProto.cpp

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=329231&r1=329230&r2=329231&view=diff
==
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Apr  4 14:09:00 2018
@@ -72,6 +72,8 @@ protected:
 
   void verifyFormat(llvm::StringRef Expected, llvm::StringRef Code,
 const FormatStyle &Style = getLLVMStyle()) {
+EXPECT_EQ(Expected.str(), format(Expected, Style))
+<< "Expected code is not stable";
 EXPECT_EQ(Expected.str(), format(Code, Style));
 if (Style.Language == FormatStyle::LK_Cpp) {
   // Objective-C++ is a superset of C++, so everything checked for C++

Modified: cfe/trunk/unittests/Format/FormatTestComments.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestComments.cpp?rev=329231&r1=329230&r2=329231&view=diff
==
--- cfe/trunk/unittests/Format/FormatTestComments.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestComments.cpp Wed Apr  4 14:09:00 2018
@@ -70,6 +70,7 @@ protected:
 
   void verifyFormat(llvm::StringRef Code,
 const FormatStyle &Style = getLLVMStyle()) {
+EXPECT_EQ(Code.str(), format(Code, Style)) << "Expected code is not 
stable";
 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
   }
 

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=329231&r1=329230&r2=329231&view=diff
==
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Wed Apr  4 14:09:00 2018
@@ -49,6 +49,8 @@ protected:
   static void verifyFormat(
   llvm::StringRef Code,
   const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
+EXPECT_EQ(Code.str(), format(Code, Style))
+<< "Expected code is not stable";
 std::string Result = format(test::messUp(Code), Style);
 EXPECT_EQ(Code.str(), Result) << "Formatted:\n" << Result;
   }
@@ -57,6 +59,8 @@ protected:
   llvm::StringRef Expected,
   llvm::StringRef Code,
   const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
+EXPECT_EQ(Expected.str(), format(Expected, Style))
+<< "Expected code is not stable";
 std::string Result = format(Code, Style);
 EXPECT_EQ(Expected.str(), Result) << "Formatted:\n" << Result;
   }

Modified: cfe/trunk/unittests/Format/FormatTestJava.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJava.cpp?rev=329231&r1=329230&r2=329231&view=diff
==
--- cfe/trunk/unittests/Format/FormatTestJava.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJava.cpp Wed Apr  4 14:09:00 2018
@@ -46,6 +46,7 @@ protected:
   static void verifyFormat(
   llvm::StringRef Code,
   const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_Java)) {
+EXPECT_EQ(Code.str(), format(Code, Style)) << "Expected code is not 
stable";
 EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
   }
 };

Modified: cfe/trunk/unittests/Format/FormatTestObjC.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestObjC.cpp?rev=329231&r1=329230&r2=329231&view=diff
==
--- cfe/trunk/unittests/Format/FormatTestObjC.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestObjC.cpp Wed Apr  4 14:09:00 2018
@@ -58,6 +58,7 @@ protected:
   }
 
   void verifyFormat(StringRef Code) {
+EXPECT_EQ(Code.str(), format(Code)) << "Expect

r322448 - [clang-format] [NFC] Remove comment

2018-01-13 Thread Mark Zeren via cfe-commits
Author: mzeren-vmw
Date: Sat Jan 13 14:52:34 2018
New Revision: 322448

URL: http://llvm.org/viewvc/llvm-project?rev=322448&view=rev
Log:
[clang-format] [NFC] Remove comment

Remove inaccurate comment that came in with  r312125.

Modified:
cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=322448&r1=322447&r2=322448&view=diff
==
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Sat Jan 13 14:52:34 2018
@@ -2484,8 +2484,6 @@ TEST_F(FormatTest, IndentPreprocessorDir
"#endif",
Style);
   // Test with include guards.
-  // EXPECT_EQ is used because verifyFormat() calls messUp() which incorrectly
-  // merges lines.
   verifyFormat("#ifndef HEADER_H\n"
"#define HEADER_H\n"
"code();\n"


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


r323904 - [clang-format] Align preprocessor comments with #

2018-01-31 Thread Mark Zeren via cfe-commits
Author: mzeren-vmw
Date: Wed Jan 31 12:05:50 2018
New Revision: 323904

URL: http://llvm.org/viewvc/llvm-project?rev=323904&view=rev
Log:
[clang-format] Align preprocessor comments with #

Summary:
r312125, which introduced preprocessor indentation, shipped with a known
issue where "indentation of comments immediately before indented
preprocessor lines is toggled on each run". For example these two forms
toggle:

  #ifndef HEADER_H
  #define HEADER_H
  #if 1
  // comment
  #   define A 0
  #endif
  #endif

  #ifndef HEADER_H
  #define HEADER_H
  #if 1
 // comment
  #   define A 0
  #endif
  #endif

This happens because we check vertical alignment against the '#' yet
indent to the level of the 'define'. This patch resolves this issue by
aligning against the '#'.

Reviewers: krasimir, klimek, djasper

Reviewed By: krasimir

Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D42408

Modified:
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=323904&r1=323903&r2=323904&view=diff
==
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Wed Jan 31 12:05:50 2018
@@ -1725,15 +1725,18 @@ void TokenAnnotator::setCommentLineLevel
   }
 }
 
-if (NextNonCommentLine && CommentLine) {
-  // If the comment is currently aligned with the line immediately 
following
-  // it, that's probably intentional and we should keep it.
-  bool AlignedWithNextLine =
-  NextNonCommentLine->First->NewlinesBefore <= 1 &&
-  NextNonCommentLine->First->OriginalColumn ==
-  (*I)->First->OriginalColumn;
-  if (AlignedWithNextLine)
-(*I)->Level = NextNonCommentLine->Level;
+// If the comment is currently aligned with the line immediately following
+// it, that's probably intentional and we should keep it.
+if (NextNonCommentLine && CommentLine &&
+NextNonCommentLine->First->NewlinesBefore <= 1 &&
+NextNonCommentLine->First->OriginalColumn ==
+(*I)->First->OriginalColumn) {
+  // Align comments for preprocessor lines with the # in column 0.
+  // Otherwise, align with the next line.
+  (*I)->Level = (NextNonCommentLine->Type == LT_PreprocessorDirective ||
+ NextNonCommentLine->Type == LT_ImportStatement)
+? 0
+: NextNonCommentLine->Level;
 } else {
   NextNonCommentLine = (*I)->First->isNot(tok::r_brace) ? (*I) : nullptr;
 }

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=323904&r1=323903&r2=323904&view=diff
==
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Jan 31 12:05:50 2018
@@ -2595,21 +2595,85 @@ TEST_F(FormatTest, IndentPreprocessorDir
"code();\n"
"#endif",
Style));
-  // FIXME: The comment indent corrector in TokenAnnotator gets thrown off by
-  // preprocessor indentation.
-  EXPECT_EQ("#if 1\n"
-"  // comment\n"
-"#  define A 0\n"
-"// comment\n"
-"#  define B 0\n"
-"#endif",
-format("#if 1\n"
-   "// comment\n"
-   "#  define A 0\n"
-   "   // comment\n"
-   "#  define B 0\n"
-   "#endif",
-   Style));
+  // Keep comments aligned with #, otherwise indent comments normally. These
+  // tests cannot use verifyFormat because messUp manipulates leading
+  // whitespace.
+  {
+const char *Expected = ""
+   "void f() {\n"
+   "#if 1\n"
+   "// Preprocessor aligned.\n"
+   "#  define A 0\n"
+   "  // Code. Separated by blank line.\n"
+   "\n"
+   "#  define B 0\n"
+   "  // Code. Not aligned with #\n"
+   "#  define C 0\n"
+   "#endif";
+const char *ToFormat = ""
+   "void f() {\n"
+   "#if 1\n"
+   "// Preprocessor aligned.\n"
+   "#  define A 0\n"
+   "// Code. Separated by blank line.\n"
+   "\n"
+   "#  define B 0\n"
+   "   // Code. Not aligned with #\n"
+   "#  define C 0\n"
+   "#endif";
+EXPEC

r324238 - [clang-format] Fixup #include guard indents after parseFile()

2018-02-05 Thread Mark Zeren via cfe-commits
Author: mzeren-vmw
Date: Mon Feb  5 06:35:54 2018
New Revision: 324238

URL: http://llvm.org/viewvc/llvm-project?rev=324238&view=rev
Log:
[clang-format] Fixup #include guard indents after parseFile()

Summary:
When a preprocessor indent closes after the last line of normal code we do not
correctly fixup include guard indents. For example:

  #ifndef HEADER_H
  #define HEADER_H
  #if 1
  int i;
  #  define A 0
  #endif
  #endif

incorrectly reformats to:

  #ifndef HEADER_H
  #define HEADER_H
  #if 1
  int i;
  #define A 0
  #  endif
  #endif

To resolve this issue we must fixup levels after parseFile(). Delaying
the fixup introduces a new state, so consolidate include guard search
state into an enum.

Reviewers: krasimir, klimek

Reviewed By: krasimir

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D42035

Modified:
cfe/trunk/lib/Format/UnwrappedLineParser.cpp
cfe/trunk/lib/Format/UnwrappedLineParser.h
cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=324238&r1=324237&r2=324238&view=diff
==
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Mon Feb  5 06:35:54 2018
@@ -234,14 +234,15 @@ UnwrappedLineParser::UnwrappedLineParser
   CurrentLines(&Lines), Style(Style), Keywords(Keywords),
   CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
   Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
-  IfNdefCondition(nullptr), FoundIncludeGuardStart(false),
-  IncludeGuardRejected(false), FirstStartColumn(FirstStartColumn) {}
+  IncludeGuard(Style.IndentPPDirectives == FormatStyle::PPDIS_None
+   ? IG_Rejected
+   : IG_Inited),
+  IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn) {}
 
 void UnwrappedLineParser::reset() {
   PPBranchLevel = -1;
-  IfNdefCondition = nullptr;
-  FoundIncludeGuardStart = false;
-  IncludeGuardRejected = false;
+  IncludeGuard = IG_Inited;
+  IncludeGuardToken = nullptr;
   Line.reset(new UnwrappedLine);
   CommentsBeforeNextToken.clear();
   FormatTok = nullptr;
@@ -264,6 +265,14 @@ void UnwrappedLineParser::parse() {
 
 readToken();
 parseFile();
+
+// If we found an include guard then all preprocessor directives (other 
than
+// the guard) are over-indented by one.
+if (IncludeGuard == IG_Found)
+  for (auto &Line : Lines)
+if (Line.InPPDirective && Line.Level > 0)
+  --Line.Level;
+
 // Create line with eof token.
 pushToken(FormatTok);
 addUnwrappedLine();
@@ -724,11 +733,11 @@ void UnwrappedLineParser::parsePPIf(bool
   // If there's a #ifndef on the first line, and the only lines before it are
   // comments, it could be an include guard.
   bool MaybeIncludeGuard = IfNDef;
-  if (!IncludeGuardRejected && !FoundIncludeGuardStart && MaybeIncludeGuard) {
+  if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
 for (auto &Line : Lines) {
   if (!Line.Tokens.front().Tok->is(tok::comment)) {
 MaybeIncludeGuard = false;
-IncludeGuardRejected = true;
+IncludeGuard = IG_Rejected;
 break;
   }
 }
@@ -736,14 +745,16 @@ void UnwrappedLineParser::parsePPIf(bool
   --PPBranchLevel;
   parsePPUnknown();
   ++PPBranchLevel;
-  if (!IncludeGuardRejected && !FoundIncludeGuardStart && MaybeIncludeGuard)
-IfNdefCondition = IfCondition;
+  if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
+IncludeGuard = IG_IfNdefed;
+IncludeGuardToken = IfCondition;
+  }
 }
 
 void UnwrappedLineParser::parsePPElse() {
   // If a potential include guard has an #else, it's not an include guard.
-  if (FoundIncludeGuardStart && PPBranchLevel == 0)
-FoundIncludeGuardStart = false;
+  if (IncludeGuard == IG_Defined && PPBranchLevel == 0)
+IncludeGuard = IG_Rejected;
   conditionalCompilationAlternative();
   if (PPBranchLevel > -1)
 --PPBranchLevel;
@@ -757,34 +768,37 @@ void UnwrappedLineParser::parsePPEndIf()
   conditionalCompilationEnd();
   parsePPUnknown();
   // If the #endif of a potential include guard is the last thing in the file,
-  // then we count it as a real include guard and subtract one from every
-  // preprocessor indent.
+  // then we found an include guard.
   unsigned TokenPosition = Tokens->getPosition();
   FormatToken *PeekNext = AllTokens[TokenPosition];
-  if (FoundIncludeGuardStart && PPBranchLevel == -1 && PeekNext->is(tok::eof) 
&&
+  if (IncludeGuard == IG_Defined && PPBranchLevel == -1 &&
+  PeekNext->is(tok::eof) &&
   Style.IndentPPDirectives != FormatStyle::PPDIS_None)
-for (auto &Line : Lines)
-  if (Line.InPPDirective && Line.Level > 0)
---Line.Level;
+IncludeGuard = IG_Found;
 }
 
 void UnwrappedLineParser::parsePPDefine(

r324239 - Revert "[clang-format] Fixup #include guard indents after parseFile()"

2018-02-05 Thread Mark Zeren via cfe-commits
Author: mzeren-vmw
Date: Mon Feb  5 06:47:04 2018
New Revision: 324239

URL: http://llvm.org/viewvc/llvm-project?rev=324239&view=rev
Log:
Revert "[clang-format] Fixup #include guard indents after parseFile()"

This reverts r324238 | mzeren-vmw | 2018-02-05 06:35:54 -0800 (Mon, 05 Feb 
2018) | 35 lines

Incorrect version pushed upstream.

Modified:
cfe/trunk/lib/Format/UnwrappedLineParser.cpp
cfe/trunk/lib/Format/UnwrappedLineParser.h
cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=324239&r1=324238&r2=324239&view=diff
==
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Mon Feb  5 06:47:04 2018
@@ -234,15 +234,14 @@ UnwrappedLineParser::UnwrappedLineParser
   CurrentLines(&Lines), Style(Style), Keywords(Keywords),
   CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
   Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
-  IncludeGuard(Style.IndentPPDirectives == FormatStyle::PPDIS_None
-   ? IG_Rejected
-   : IG_Inited),
-  IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn) {}
+  IfNdefCondition(nullptr), FoundIncludeGuardStart(false),
+  IncludeGuardRejected(false), FirstStartColumn(FirstStartColumn) {}
 
 void UnwrappedLineParser::reset() {
   PPBranchLevel = -1;
-  IncludeGuard = IG_Inited;
-  IncludeGuardToken = nullptr;
+  IfNdefCondition = nullptr;
+  FoundIncludeGuardStart = false;
+  IncludeGuardRejected = false;
   Line.reset(new UnwrappedLine);
   CommentsBeforeNextToken.clear();
   FormatTok = nullptr;
@@ -265,14 +264,6 @@ void UnwrappedLineParser::parse() {
 
 readToken();
 parseFile();
-
-// If we found an include guard then all preprocessor directives (other 
than
-// the guard) are over-indented by one.
-if (IncludeGuard == IG_Found)
-  for (auto &Line : Lines)
-if (Line.InPPDirective && Line.Level > 0)
-  --Line.Level;
-
 // Create line with eof token.
 pushToken(FormatTok);
 addUnwrappedLine();
@@ -733,11 +724,11 @@ void UnwrappedLineParser::parsePPIf(bool
   // If there's a #ifndef on the first line, and the only lines before it are
   // comments, it could be an include guard.
   bool MaybeIncludeGuard = IfNDef;
-  if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
+  if (!IncludeGuardRejected && !FoundIncludeGuardStart && MaybeIncludeGuard) {
 for (auto &Line : Lines) {
   if (!Line.Tokens.front().Tok->is(tok::comment)) {
 MaybeIncludeGuard = false;
-IncludeGuard = IG_Rejected;
+IncludeGuardRejected = true;
 break;
   }
 }
@@ -745,16 +736,14 @@ void UnwrappedLineParser::parsePPIf(bool
   --PPBranchLevel;
   parsePPUnknown();
   ++PPBranchLevel;
-  if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
-IncludeGuard = IG_IfNdefed;
-IncludeGuardToken = IfCondition;
-  }
+  if (!IncludeGuardRejected && !FoundIncludeGuardStart && MaybeIncludeGuard)
+IfNdefCondition = IfCondition;
 }
 
 void UnwrappedLineParser::parsePPElse() {
   // If a potential include guard has an #else, it's not an include guard.
-  if (IncludeGuard == IG_Defined && PPBranchLevel == 0)
-IncludeGuard = IG_Rejected;
+  if (FoundIncludeGuardStart && PPBranchLevel == 0)
+FoundIncludeGuardStart = false;
   conditionalCompilationAlternative();
   if (PPBranchLevel > -1)
 --PPBranchLevel;
@@ -768,37 +757,34 @@ void UnwrappedLineParser::parsePPEndIf()
   conditionalCompilationEnd();
   parsePPUnknown();
   // If the #endif of a potential include guard is the last thing in the file,
-  // then we found an include guard.
+  // then we count it as a real include guard and subtract one from every
+  // preprocessor indent.
   unsigned TokenPosition = Tokens->getPosition();
   FormatToken *PeekNext = AllTokens[TokenPosition];
-  if (IncludeGuard == IG_Defined && PPBranchLevel == -1 &&
-  PeekNext->is(tok::eof) &&
+  if (FoundIncludeGuardStart && PPBranchLevel == -1 && PeekNext->is(tok::eof) 
&&
   Style.IndentPPDirectives != FormatStyle::PPDIS_None)
-IncludeGuard = IG_Found;
+for (auto &Line : Lines)
+  if (Line.InPPDirective && Line.Level > 0)
+--Line.Level;
 }
 
 void UnwrappedLineParser::parsePPDefine() {
   nextToken();
 
   if (FormatTok->Tok.getKind() != tok::identifier) {
-IncludeGuard = IG_Rejected;
-IncludeGuardToken = nullptr;
 parsePPUnknown();
 return;
   }
-
-  if (IncludeGuard == IG_IfNdefed &&
-  IncludeGuardToken->TokenText == FormatTok->TokenText) {
-IncludeGuard = IG_Defined;
-IncludeGuardToken = nullptr;
+  if (IfNdefCondition && IfNdefCondition->TokenText == FormatTok->TokenText) {
+FoundIncludeGuardStart = true;
 for (auto &Line : Lines) {
   i

r324246 - [clang-format] Re-land: Fixup #include guard indents after parseFile()

2018-02-05 Thread Mark Zeren via cfe-commits
Author: mzeren-vmw
Date: Mon Feb  5 07:59:00 2018
New Revision: 324246

URL: http://llvm.org/viewvc/llvm-project?rev=324246&view=rev
Log:
[clang-format] Re-land: Fixup #include guard indents after parseFile()

Summary:
When a preprocessor indent closes after the last line of normal code we do not
correctly fixup include guard indents. For example:

  #ifndef HEADER_H
  #define HEADER_H
  #if 1
  int i;
  #  define A 0
  #endif
  #endif

incorrectly reformats to:

  #ifndef HEADER_H
  #define HEADER_H
  #if 1
  int i;
  #define A 0
  #  endif
  #endif

To resolve this issue we must fixup levels after parseFile(). Delaying
the fixup introduces a new state, so consolidate include guard search
state into an enum.

Reviewers: krasimir, klimek

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D42035

Modified:
cfe/trunk/lib/Format/UnwrappedLineParser.cpp
cfe/trunk/lib/Format/UnwrappedLineParser.h
cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=324246&r1=324245&r2=324246&view=diff
==
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Mon Feb  5 07:59:00 2018
@@ -234,14 +234,17 @@ UnwrappedLineParser::UnwrappedLineParser
   CurrentLines(&Lines), Style(Style), Keywords(Keywords),
   CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
   Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
-  IfNdefCondition(nullptr), FoundIncludeGuardStart(false),
-  IncludeGuardRejected(false), FirstStartColumn(FirstStartColumn) {}
+  IncludeGuard(Style.IndentPPDirectives == FormatStyle::PPDIS_None
+   ? IG_Rejected
+   : IG_Inited),
+  IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn) {}
 
 void UnwrappedLineParser::reset() {
   PPBranchLevel = -1;
-  IfNdefCondition = nullptr;
-  FoundIncludeGuardStart = false;
-  IncludeGuardRejected = false;
+  IncludeGuard = Style.IndentPPDirectives == FormatStyle::PPDIS_None
+ ? IG_Rejected
+ : IG_Inited;
+  IncludeGuardToken = nullptr;
   Line.reset(new UnwrappedLine);
   CommentsBeforeNextToken.clear();
   FormatTok = nullptr;
@@ -264,6 +267,14 @@ void UnwrappedLineParser::parse() {
 
 readToken();
 parseFile();
+
+// If we found an include guard then all preprocessor directives (other 
than
+// the guard) are over-indented by one.
+if (IncludeGuard == IG_Found)
+  for (auto &Line : Lines)
+if (Line.InPPDirective && Line.Level > 0)
+  --Line.Level;
+
 // Create line with eof token.
 pushToken(FormatTok);
 addUnwrappedLine();
@@ -724,26 +735,27 @@ void UnwrappedLineParser::parsePPIf(bool
   // If there's a #ifndef on the first line, and the only lines before it are
   // comments, it could be an include guard.
   bool MaybeIncludeGuard = IfNDef;
-  if (!IncludeGuardRejected && !FoundIncludeGuardStart && MaybeIncludeGuard) {
+  if (IncludeGuard == IG_Inited && MaybeIncludeGuard)
 for (auto &Line : Lines) {
   if (!Line.Tokens.front().Tok->is(tok::comment)) {
 MaybeIncludeGuard = false;
-IncludeGuardRejected = true;
+IncludeGuard = IG_Rejected;
 break;
   }
 }
-  }
   --PPBranchLevel;
   parsePPUnknown();
   ++PPBranchLevel;
-  if (!IncludeGuardRejected && !FoundIncludeGuardStart && MaybeIncludeGuard)
-IfNdefCondition = IfCondition;
+  if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
+IncludeGuard = IG_IfNdefed;
+IncludeGuardToken = IfCondition;
+  }
 }
 
 void UnwrappedLineParser::parsePPElse() {
   // If a potential include guard has an #else, it's not an include guard.
-  if (FoundIncludeGuardStart && PPBranchLevel == 0)
-FoundIncludeGuardStart = false;
+  if (IncludeGuard == IG_Defined && PPBranchLevel == 0)
+IncludeGuard = IG_Rejected;
   conditionalCompilationAlternative();
   if (PPBranchLevel > -1)
 --PPBranchLevel;
@@ -757,34 +769,37 @@ void UnwrappedLineParser::parsePPEndIf()
   conditionalCompilationEnd();
   parsePPUnknown();
   // If the #endif of a potential include guard is the last thing in the file,
-  // then we count it as a real include guard and subtract one from every
-  // preprocessor indent.
+  // then we found an include guard.
   unsigned TokenPosition = Tokens->getPosition();
   FormatToken *PeekNext = AllTokens[TokenPosition];
-  if (FoundIncludeGuardStart && PPBranchLevel == -1 && PeekNext->is(tok::eof) 
&&
+  if (IncludeGuard == IG_Defined && PPBranchLevel == -1 &&
+  PeekNext->is(tok::eof) &&
   Style.IndentPPDirectives != FormatStyle::PPDIS_None)
-for (auto &Line : Lines)
-  if (Line.InPPDirective && Line.Level > 0)
---Line.Level;
+IncludeGuard = IG_Found;
 }
 
 vo