[cfe-commits] r173038 - in /cfe/trunk: lib/Format/UnwrappedLineParser.cpp unittests/Format/FormatTest.cpp

Manuel Klimek klimek at google.com
Mon Jan 21 05:58:54 PST 2013


Author: klimek
Date: Mon Jan 21 07:58:54 2013
New Revision: 173038

URL: http://llvm.org/viewvc/llvm-project?rev=173038&view=rev
Log:
Fixes detection of class template specializations.

Now correctly formats:
template <> class A<int> {} a;

Modified:
    cfe/trunk/lib/Format/UnwrappedLineParser.cpp
    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=173038&r1=173037&r2=173038&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Mon Jan 21 07:58:54 2013
@@ -620,7 +620,17 @@
            FormatTok.Tok.is(tok::coloncolon))
       nextToken();
 
-    if (FormatTok.Tok.is(tok::colon)) {
+    // Note that parsing away template declarations here leads to incorrectly
+    // accepting function declarations as record declarations.
+    // In general, we cannot solve this problem. Consider:
+    // class A<int> B() {}
+    // which can be a function definition or a class definition when B() is a
+    // macro. If we find enough real-world cases where this is a problem, we
+    // can parse for the 'template' keyword in the beginning of the statement,
+    // and thus rule out the record production in case there is no template
+    // (this would still leave us with an ambiguity between template function
+    // and class declarations).
+    if (FormatTok.Tok.is(tok::colon) || FormatTok.Tok.is(tok::less)) {
       while (FormatTok.Tok.isNot(tok::l_brace)) {
         if (FormatTok.Tok.is(tok::semi))
           return;
@@ -630,6 +640,9 @@
   }
   if (FormatTok.Tok.is(tok::l_brace))
     parseBlock();
+  // We fall through to parsing a structural element afterwards, so
+  // class A {} n, m;
+  // will end up in one unwrapped line.
 }
 
 void UnwrappedLineParser::parseObjCProtocolList() {

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=173038&r1=173037&r2=173038&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Jan 21 07:58:54 2013
@@ -1530,6 +1530,14 @@
   // Redefinition from nested context:
   verifyFormat("class A::B::C {} n;");
 
+  // Template definitions.
+  // FIXME: This is still incorrectly handled at the formatter side.
+  verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {};");
+
+  // FIXME:
+  // This now gets parsed incorrectly as class definition.
+  // verifyFormat("class A<int> f() {}\nint n;");
+
   // Elaborate types where incorrectly parsing the structural element would
   // break the indent.
   verifyFormat("if (true)\n"





More information about the cfe-commits mailing list