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

Mark Zeren via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 5 06:47:04 PST 2018


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) {
       if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) {
-        IncludeGuard = IG_Rejected;
+        FoundIncludeGuardStart = false;
         break;
       }
     }
   }
-
+  IfNdefCondition = nullptr;
   nextToken();
   if (FormatTok->Tok.getKind() == tok::l_paren &&
       FormatTok->WhitespaceRange.getBegin() ==
@@ -825,6 +811,7 @@ void UnwrappedLineParser::parsePPUnknown
   if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash)
     Line->Level += PPBranchLevel + 1;
   addUnwrappedLine();
+  IfNdefCondition = nullptr;
 }
 
 // Here we blacklist certain tokens that are not usually the first token in an

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.h?rev=324239&r1=324238&r2=324239&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.h (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.h Mon Feb  5 06:47:04 2018
@@ -248,23 +248,10 @@ private:
   // sequence.
   std::stack<int> PPChainBranchIndex;
 
-  // Include guard search state. Used to fixup preprocessor indent levels
-  // so that include guards do not participate in indentation.
-  enum IncludeGuardState {
-    IG_Inited,
-    IG_IfNdefed,
-    IG_Defined,
-    IG_Found,
-    IG_Rejected,
-  };
-
-  // Current state of include guard search.
-  IncludeGuardState IncludeGuard;
-
-  // Points to the #ifndef condition for a potential include guard. Null unless
-  // IncludeGuardState == IG_IfNdefed.
-  FormatToken *IncludeGuardToken;
-
+  // Contains the #ifndef condition for a potential include guard.
+  FormatToken *IfNdefCondition;
+  bool FoundIncludeGuardStart;
+  bool IncludeGuardRejected;
   // Contains the first start column where the source begins. This is zero for
   // normal source code and may be nonzero when formatting a code fragment that
   // does not start at the beginning of the file.

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=324239&r1=324238&r2=324239&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Feb  5 06:47:04 2018
@@ -2547,20 +2547,6 @@ TEST_F(FormatTest, IndentPreprocessorDir
                "#elif FOO\n"
                "#endif",
                Style);
-  // Non-identifier #define after potential include guard.
-  verifyFormat("#ifndef FOO\n"
-               "#  define 1\n"
-               "#endif\n",
-               Style);
-  // #if closes past last non-preprocessor line.
-  verifyFormat("#ifndef FOO\n"
-               "#define FOO\n"
-               "#if 1\n"
-               "int i;\n"
-               "#  define A 0\n"
-               "#endif\n"
-               "#endif\n",
-               Style);
   // FIXME: This doesn't handle the case where there's code between the
   // #ifndef and #define but all other conditions hold. This is because when
   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the




More information about the cfe-commits mailing list