diff options
author | Chris Lattner <sabre@nondot.org> | 2009-04-18 22:05:41 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2009-04-18 22:05:41 +0000 |
commit | 24f0e48c0aa62f2268e061aad70f9b19a59e7b52 (patch) | |
tree | 3581af4268900f24201d9fc9126fe49afbad4440 /lib/Lex/Lexer.cpp | |
parent | 0edfab68d463201ac2933cf88243701dbc3ba5ab (diff) |
factor escape newline measuring out into its own helper function.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@69482 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Lex/Lexer.cpp')
-rw-r--r-- | lib/Lex/Lexer.cpp | 85 |
1 files changed, 45 insertions, 40 deletions
diff --git a/lib/Lex/Lexer.cpp b/lib/Lex/Lexer.cpp index acdefe55e4..b9aa0178c6 100644 --- a/lib/Lex/Lexer.cpp +++ b/lib/Lex/Lexer.cpp @@ -399,6 +399,30 @@ static char DecodeTrigraphChar(const char *CP, Lexer *L) { return Res; } +/// getEscapedNewLineSize - Return the size of the specified escaped newline, +/// or 0 if it is not an escaped newline. P[-1] is known to be a "\" or a +/// trigraph equivalent on entry to this function. +unsigned Lexer::getEscapedNewLineSize(const char *Ptr) { + unsigned Size = 0; + while (isWhitespace(Ptr[Size])) { + ++Size; + + if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r') + continue; + + // If this is a \r\n or \n\r, skip the other half. + if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') && + Ptr[Size-1] != Ptr[Size]) + ++Size; + + return Size; + } + + // Not an escaped newline, must be a \t or something else. + return 0; +} + + /// getCharAndSizeSlow - Peek a single 'character' from the specified buffer, /// get its size, and return it. This is tricky in several cases: /// 1. If currently at the start of a trigraph, we warn about the trigraph, @@ -427,29 +451,20 @@ Slash: if (!isWhitespace(Ptr[0])) return '\\'; // See if we have optional whitespace characters followed by a newline. - unsigned SizeTmp = 0; - do { - ++SizeTmp; - if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') { - // Remember that this token needs to be cleaned. - if (Tok) Tok->setFlag(Token::NeedsCleaning); - - // Warn if there was whitespace between the backslash and newline. - if (SizeTmp != 1 && Tok && !isLexingRawMode()) - Diag(Ptr, diag::backslash_newline_space); - - // If this is a \r\n or \n\r, skip the newlines. - if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') && - Ptr[SizeTmp-1] != Ptr[SizeTmp]) - ++SizeTmp; + if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) { + // Remember that this token needs to be cleaned. + if (Tok) Tok->setFlag(Token::NeedsCleaning); + + // Warn if there was whitespace between the backslash and newline. + if (EscapedNewLineSize != 1 && Tok && !isLexingRawMode()) + Diag(Ptr, diag::backslash_newline_space); - // Found backslash<whitespace><newline>. Parse the char after it. - Size += SizeTmp; - Ptr += SizeTmp; - // Use slow version to accumulate a correct size field. - return getCharAndSizeSlow(Ptr, Size, Tok); - } - } while (isWhitespace(Ptr[SizeTmp])); + // Found backslash<whitespace><newline>. Parse the char after it. + Size += EscapedNewLineSize; + Ptr += EscapedNewLineSize; + // Use slow version to accumulate a correct size field. + return getCharAndSizeSlow(Ptr, Size, Tok); + } // Otherwise, this is not an escaped newline, just return the slash. return '\\'; @@ -493,24 +508,14 @@ Slash: if (!isWhitespace(Ptr[0])) return '\\'; // See if we have optional whitespace characters followed by a newline. - unsigned SizeTmp = 0; - do { - ++SizeTmp; - if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') { - - // If this is a \r\n or \n\r, skip the newlines. - if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') && - Ptr[SizeTmp-1] != Ptr[SizeTmp]) - ++SizeTmp; - - // Found backslash<whitespace><newline>. Parse the char after it. - Size += SizeTmp; - Ptr += SizeTmp; - - // Use slow version to accumulate a correct size field. - return getCharAndSizeSlowNoWarn(Ptr, Size, Features); - } - } while (isWhitespace(Ptr[SizeTmp])); + if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) { + // Found backslash<whitespace><newline>. Parse the char after it. + Size += EscapedNewLineSize; + Ptr += EscapedNewLineSize; + + // Use slow version to accumulate a correct size field. + return getCharAndSizeSlowNoWarn(Ptr, Size, Features); + } // Otherwise, this is not an escaped newline, just return the slash. return '\\'; |