From 6f8424b79863fc9810393c95ecdee97895ab6f26 Mon Sep 17 00:00:00 2001 From: Manuel Klimek Date: Sat, 5 Jan 2013 21:34:55 +0000 Subject: [PATCH] Fixes PR14811: Crash when formatting some macros A preprocessor directive cannot be started while we're parsing one. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@171635 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/UnwrappedLineParser.cpp | 2 +- unittests/Format/FormatTest.cpp | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index 04d1b36582..c1bafa9a3b 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -469,7 +469,7 @@ void UnwrappedLineParser::nextToken() { void UnwrappedLineParser::readToken() { FormatTok = Tokens->getNextToken(); - while (FormatTok.Tok.is(tok::hash)) { + while (!Line.InPPDirective && FormatTok.Tok.is(tok::hash)) { // FIXME: This is incorrect - the correct way is to create a // data structure that will construct the parts around the preprocessor // directive as a structured \c UnwrappedLine. diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index d5c7978e7b..89fe71c5ba 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -458,6 +458,14 @@ TEST_F(FormatTest, MacroDefinitionInsideStatement) { EXPECT_EQ("int x,\n#define A\ny;", format("int x,\n#define A\ny;")); } +TEST_F(FormatTest, HashInMacroDefinition) { + verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); + verifyFormat("#define A \\\n" + " { \\\n" + " f(#c);\\\n" + " }", getLLVMStyleWithColumns(11)); +} + // 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 -- 2.40.0