Bigcheese created this revision. Bigcheese added reviewers: thakis, djasper. Bigcheese added a project: clang-format. Herald added a subscriber: dexonsmith. Herald added a project: clang.
Previously things like: int main() { @try { } @catch (NSException *e) { } @try { } @catch (NSException *e) { } } Would be formatted like: int main() { @try { } @catch (NSException *e) { } @ try { } @catch (NSException *e) { } } because `UnwrappedLineParser::parseTryCatch()` would consume the `@` as part of checking for another `catch` or `finally`. This patch fixes that by doing a lookahead. I'm not super happy about the way the lookhead works, but this is the only thing that worked. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D71239 Files: clang/lib/Format/UnwrappedLineParser.cpp clang/unittests/Format/FormatTestObjC.cpp Index: clang/unittests/Format/FormatTestObjC.cpp =================================================================== --- clang/unittests/Format/FormatTestObjC.cpp +++ clang/unittests/Format/FormatTestObjC.cpp @@ -191,7 +191,13 @@ " @throw;\n" "} @finally {\n" " exit(42);\n" - "}"); + "}\n" + "@try {\n" + "} @catch (NSException *e) {\n" + "}\n" + "@ /* adena */ try {\n" + "} @catch (NSException *e) {\n" + "}\n"); verifyFormat("DEBUG({\n" " @try {\n" " } @finally {\n" Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -1840,16 +1840,27 @@ --Line->Level; } while (1) { - if (FormatTok->is(tok::at)) - nextToken(); + unsigned StoredPosition = Tokens->getPosition(); + if (FormatTok->is(tok::at)) { + // Get next non-comment token. + do { + FormatTok = Tokens->getNextToken(); + } while (FormatTok->is(tok::comment)); + } if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except, tok::kw___finally) || ((Style.Language == FormatStyle::LK_Java || Style.Language == FormatStyle::LK_JavaScript) && FormatTok->is(Keywords.kw_finally)) || (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) || - FormatTok->Tok.isObjCAtKeyword(tok::objc_finally)))) + FormatTok->Tok.isObjCAtKeyword(tok::objc_finally)))) { + // Go back to before the (optional) @. + FormatTok = Tokens->setPosition(StoredPosition); break; + } + FormatTok = Tokens->setPosition(StoredPosition); + if (FormatTok->is(tok::at)) + nextToken(); nextToken(); while (FormatTok->isNot(tok::l_brace)) { if (FormatTok->is(tok::l_paren)) {
Index: clang/unittests/Format/FormatTestObjC.cpp =================================================================== --- clang/unittests/Format/FormatTestObjC.cpp +++ clang/unittests/Format/FormatTestObjC.cpp @@ -191,7 +191,13 @@ " @throw;\n" "} @finally {\n" " exit(42);\n" - "}"); + "}\n" + "@try {\n" + "} @catch (NSException *e) {\n" + "}\n" + "@ /* adena */ try {\n" + "} @catch (NSException *e) {\n" + "}\n"); verifyFormat("DEBUG({\n" " @try {\n" " } @finally {\n" Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -1840,16 +1840,27 @@ --Line->Level; } while (1) { - if (FormatTok->is(tok::at)) - nextToken(); + unsigned StoredPosition = Tokens->getPosition(); + if (FormatTok->is(tok::at)) { + // Get next non-comment token. + do { + FormatTok = Tokens->getNextToken(); + } while (FormatTok->is(tok::comment)); + } if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except, tok::kw___finally) || ((Style.Language == FormatStyle::LK_Java || Style.Language == FormatStyle::LK_JavaScript) && FormatTok->is(Keywords.kw_finally)) || (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) || - FormatTok->Tok.isObjCAtKeyword(tok::objc_finally)))) + FormatTok->Tok.isObjCAtKeyword(tok::objc_finally)))) { + // Go back to before the (optional) @. + FormatTok = Tokens->setPosition(StoredPosition); break; + } + FormatTok = Tokens->setPosition(StoredPosition); + if (FormatTok->is(tok::at)) + nextToken(); nextToken(); while (FormatTok->isNot(tok::l_brace)) { if (FormatTok->is(tok::l_paren)) {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits