forked from OSchip/llvm-project
factor escape newline measuring out into its own helper function.
llvm-svn: 69482
This commit is contained in:
parent
dfbfc44df7
commit
fbce7aa1f4
|
@ -336,6 +336,12 @@ public:
|
||||||
Size = 0;
|
Size = 0;
|
||||||
return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
|
return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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 "\" on entry
|
||||||
|
/// to this function.
|
||||||
|
static unsigned getEscapedNewLineSize(const char *P);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
/// getCharAndSizeSlowNoWarn - Same as getCharAndSizeSlow, but never emits a
|
/// getCharAndSizeSlowNoWarn - Same as getCharAndSizeSlow, but never emits a
|
||||||
|
|
|
@ -399,6 +399,30 @@ static char DecodeTrigraphChar(const char *CP, Lexer *L) {
|
||||||
return Res;
|
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,
|
/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
|
||||||
/// get its size, and return it. This is tricky in several cases:
|
/// 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,
|
/// 1. If currently at the start of a trigraph, we warn about the trigraph,
|
||||||
|
@ -427,29 +451,20 @@ Slash:
|
||||||
if (!isWhitespace(Ptr[0])) return '\\';
|
if (!isWhitespace(Ptr[0])) return '\\';
|
||||||
|
|
||||||
// See if we have optional whitespace characters followed by a newline.
|
// See if we have optional whitespace characters followed by a newline.
|
||||||
unsigned SizeTmp = 0;
|
if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
|
||||||
do {
|
|
||||||
++SizeTmp;
|
|
||||||
if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
|
|
||||||
// Remember that this token needs to be cleaned.
|
// Remember that this token needs to be cleaned.
|
||||||
if (Tok) Tok->setFlag(Token::NeedsCleaning);
|
if (Tok) Tok->setFlag(Token::NeedsCleaning);
|
||||||
|
|
||||||
// Warn if there was whitespace between the backslash and newline.
|
// Warn if there was whitespace between the backslash and newline.
|
||||||
if (SizeTmp != 1 && Tok && !isLexingRawMode())
|
if (EscapedNewLineSize != 1 && Tok && !isLexingRawMode())
|
||||||
Diag(Ptr, diag::backslash_newline_space);
|
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;
|
|
||||||
|
|
||||||
// Found backslash<whitespace><newline>. Parse the char after it.
|
// Found backslash<whitespace><newline>. Parse the char after it.
|
||||||
Size += SizeTmp;
|
Size += EscapedNewLineSize;
|
||||||
Ptr += SizeTmp;
|
Ptr += EscapedNewLineSize;
|
||||||
// Use slow version to accumulate a correct size field.
|
// Use slow version to accumulate a correct size field.
|
||||||
return getCharAndSizeSlow(Ptr, Size, Tok);
|
return getCharAndSizeSlow(Ptr, Size, Tok);
|
||||||
}
|
}
|
||||||
} while (isWhitespace(Ptr[SizeTmp]));
|
|
||||||
|
|
||||||
// Otherwise, this is not an escaped newline, just return the slash.
|
// Otherwise, this is not an escaped newline, just return the slash.
|
||||||
return '\\';
|
return '\\';
|
||||||
|
@ -493,24 +508,14 @@ Slash:
|
||||||
if (!isWhitespace(Ptr[0])) return '\\';
|
if (!isWhitespace(Ptr[0])) return '\\';
|
||||||
|
|
||||||
// See if we have optional whitespace characters followed by a newline.
|
// See if we have optional whitespace characters followed by a newline.
|
||||||
unsigned SizeTmp = 0;
|
if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
|
||||||
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.
|
// Found backslash<whitespace><newline>. Parse the char after it.
|
||||||
Size += SizeTmp;
|
Size += EscapedNewLineSize;
|
||||||
Ptr += SizeTmp;
|
Ptr += EscapedNewLineSize;
|
||||||
|
|
||||||
// Use slow version to accumulate a correct size field.
|
// Use slow version to accumulate a correct size field.
|
||||||
return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
|
return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
|
||||||
}
|
}
|
||||||
} while (isWhitespace(Ptr[SizeTmp]));
|
|
||||||
|
|
||||||
// Otherwise, this is not an escaped newline, just return the slash.
|
// Otherwise, this is not an escaped newline, just return the slash.
|
||||||
return '\\';
|
return '\\';
|
||||||
|
|
Loading…
Reference in New Issue