]> granicus.if.org Git - clang/commitdiff
Fixes PR14811: Crash when formatting some macros
authorManuel Klimek <klimek@google.com>
Sat, 5 Jan 2013 21:34:55 +0000 (21:34 +0000)
committerManuel Klimek <klimek@google.com>
Sat, 5 Jan 2013 21:34:55 +0000 (21:34 +0000)
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
unittests/Format/FormatTest.cpp

index 04d1b365821b2568c403635f0b208e44667bd983..c1bafa9a3b1bb1e6e6df699654affc71e341a884 100644 (file)
@@ -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.
index d5c7978e7b954aa3906f5cd9d4856b0e7eb9c9d8..89fe71c5bab6746376851b98918a65f69bc626f5 100644 (file)
@@ -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