]> granicus.if.org Git - clang/commitdiff
Fixes PR14801 - preprocessor directives shouldn't be indented
authorManuel Klimek <klimek@google.com>
Sat, 5 Jan 2013 22:14:16 +0000 (22:14 +0000)
committerManuel Klimek <klimek@google.com>
Sat, 5 Jan 2013 22:14:16 +0000 (22:14 +0000)
Uses indent 0 for macros for now and resets the indent state to the
level prior to the preprocessor directive.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@171639 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/UnwrappedLineParser.cpp
lib/Format/UnwrappedLineParser.h
unittests/Format/FormatTest.cpp

index c1bafa9a3b1bb1e6e6df699654affc71e341a884..2f3a6034bcff8f029511a38f4edd200e034d9903 100644 (file)
@@ -27,10 +27,11 @@ public:
   ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
                    FormatToken &ResetToken)
       : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
-        PreviousTokenSource(TokenSource) {
+        PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource) {
     TokenSource = this;
     // FIXME: Back up all other state (errors, line indent, etc) and reset after
     // parsing the macro.
+    Line.Level = 0;
     Line.InPPDirective = true;
   }
 
@@ -38,7 +39,7 @@ public:
     TokenSource = PreviousTokenSource;
     ResetToken = Token;
     Line.InPPDirective = false;
-    Line.Level = 0;  // FIXME: Test + this is obviously incorrect
+    Line.Level = PreviousLineLevel;
   }
 
   virtual FormatToken getNextToken() {
@@ -65,7 +66,7 @@ private:
   UnwrappedLine &Line;
   FormatTokenSource *&TokenSource;
   FormatToken &ResetToken;
-
+  unsigned PreviousLineLevel;
   FormatTokenSource *PreviousTokenSource;
 
   FormatToken Token;
index 69ac7683254ac46189b967eaacd5a1b6060b19d2..a8e5b739bc87d8f7ec383a9aaece8ee64f11dded 100644 (file)
@@ -124,6 +124,9 @@ private:
   void nextToken();
   void readToken();
 
+  // FIXME: We are constantly running into bugs where Line.Level is incorrectly
+  // subtracted from beyond 0. Introduce a method to subtract from Line.Level
+  // and use that everywhere in the Parser.
   UnwrappedLine Line;
   FormatToken FormatTok;
 
index 89fe71c5bab6746376851b98918a65f69bc626f5..2165056a7f1dfcefad844641c84d683c8f6363d3 100644 (file)
@@ -466,9 +466,12 @@ TEST_F(FormatTest, HashInMacroDefinition) {
                "  }", getLLVMStyleWithColumns(11));
 }
 
+TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
+  EXPECT_EQ("{\n  {\n#define A\n  }\n}", format("{{\n#define A\n}}"));
+}
+
 // FIXME: write test for unbalanced braces in macros...
-// FIXME: test { { #include "a.h" } }
-// FIXME: test # in the middle of a statement without \n before it
+// FIXME: test # inside a normal statement (like {#define A b})
 
 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
   EXPECT_EQ(