]> granicus.if.org Git - clang/commitdiff
clang-format: No spaces around directory specifiers
authorDaniel Jasper <djasper@google.com>
Fri, 21 Dec 2012 17:58:39 +0000 (17:58 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 21 Dec 2012 17:58:39 +0000 (17:58 +0000)
This fixes PR14683. We used to format like this:
  #include <a / b>

And this patch changes this to:
  #include <a/b>

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

lib/Format/Format.cpp
unittests/Format/FormatTest.cpp

index 6983f73499b26a3993df5fc7cf3c6ab8174ecb5a..b5e4a7003d2094ff62698ac535c3fedde30feb9e 100644 (file)
@@ -42,6 +42,7 @@ struct TokenAnnotation {
     TT_CtorInitializerColon,
     TT_LineComment,
     TT_BlockComment,
+    TT_DirectorySeparator,
     TT_ObjCMethodSpecifier
   };
 
@@ -560,7 +561,36 @@ public:
       }
     }
 
+    void parseIncludeDirective() {
+      while (Index < Tokens.size()) {
+        if (Tokens[Index].Tok.is(tok::slash))
+          Annotations[Index].Type = TokenAnnotation::TT_DirectorySeparator;
+        else if (Tokens[Index].Tok.is(tok::less))
+          Annotations[Index].Type = TokenAnnotation::TT_TemplateOpener;
+        else if (Tokens[Index].Tok.is(tok::greater))
+          Annotations[Index].Type = TokenAnnotation::TT_TemplateCloser;
+        next();
+      }
+    }
+
+    void parsePreprocessorDirective() {
+      next();
+      if (Index >= Tokens.size())
+        return;
+      switch (Tokens[Index].Tok.getIdentifierInfo()->getPPKeywordID()) {
+      case tok::pp_include:
+        parseIncludeDirective();
+        break;
+      default:
+        break;
+      }
+    }
+
     void parseLine() {
+      if (Tokens[Index].Tok.is(tok::hash)) {
+        parsePreprocessorDirective();
+        return;
+      }
       while (Index < Tokens.size()) {
         consumeToken();
       }
@@ -638,6 +668,10 @@ public:
           Annotation.SpaceRequiredBefore = Style.SplitTemplateClosingGreater;
         else
           Annotation.SpaceRequiredBefore = false;
+      } else if (
+          Annotation.Type == TokenAnnotation::TT_DirectorySeparator ||
+          Annotations[i - 1].Type == TokenAnnotation::TT_DirectorySeparator) {
+        Annotation.SpaceRequiredBefore = false;
       } else if (
           Annotation.Type == TokenAnnotation::TT_BinaryOperator ||
           Annotations[i - 1].Type == TokenAnnotation::TT_BinaryOperator) {
@@ -714,7 +748,7 @@ private:
   }
 
   bool isBinaryOperator(const FormatToken &Tok) {
-    // Comma is a binary operator, but does not behave a such wrt. formatting.
+    // Comma is a binary operator, but does not behave as such wrt. formatting.
     return getBinOpPrecedence(Tok.Tok.getKind(), true, true) > prec::Comma;
   }
 
@@ -865,7 +899,8 @@ public:
     }
 
     if (FormatTok.Tok.is(tok::raw_identifier)) {
-      const IdentifierInfo &Info = IdentTable.get(tokenText(FormatTok.Tok));
+      IdentifierInfo &Info = IdentTable.get(tokenText(FormatTok.Tok));
+      FormatTok.Tok.setIdentifierInfo(&Info);
       FormatTok.Tok.setKind(Info.getTokenID());
     }
 
index c8088e2c0a7c3829fbda9c2e74899a03f235a7b7..a08f7c6cf717709980c9afc1bb3cc52103158bfd 100644 (file)
@@ -613,6 +613,7 @@ TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
 
 TEST_F(FormatTest, HandlesIncludeDirectives) {
   EXPECT_EQ("#include <string>\n", format("#include <string>\n"));
+  EXPECT_EQ("#include <a/b/c.h>\n", format("#include <a/b/c.h>\n"));
   EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n"));
   EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n"));
   EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n"));