diff options
author | Manuel Klimek <klimek@google.com> | 2013-01-21 13:58:54 +0000 |
---|---|---|
committer | Manuel Klimek <klimek@google.com> | 2013-01-21 13:58:54 +0000 |
commit | 3a3408cceb438af7cdebb5b3d938abb916162bb4 (patch) | |
tree | 0f92b45d748264aed2fd54615eb91d27c12d62f8 | |
parent | 649c7316aa29181df7270732722fe5d07ab3c7ad (diff) |
Fixes detection of class template specializations.
Now correctly formats:
template <> class A<int> {} a;
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@173038 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/Format/UnwrappedLineParser.cpp | 15 | ||||
-rw-r--r-- | unittests/Format/FormatTest.cpp | 8 |
2 files changed, 22 insertions, 1 deletions
diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index dffc309a09..7f9e97eab0 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -620,7 +620,17 @@ void UnwrappedLineParser::parseRecord() { 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 @@ void UnwrappedLineParser::parseRecord() { } 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() { diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 3c929a3f53..4725889247 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -1530,6 +1530,14 @@ TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { // 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" |