diff options
author | Richard Smith <richard-llvm@metafoo.co.uk> | 2018-04-30 05:25:48 +0000 |
---|---|---|
committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2018-04-30 05:25:48 +0000 |
commit | ef91bd38cd94e34b4b0a30e225e507f5c10087d3 (patch) | |
tree | a32eb01379c5c7f1f1bcd6a2528cabeedda12c80 /include/clang/Lex | |
parent | 9ab08c3357404e43c970e2e7438c796dd33f4593 (diff) |
PR37189 Fix incorrect end source location and spelling for a split '>>' token.
When a '>>' token is split into two '>' tokens (in C++11 onwards), or (as an
extension) when we do the same for other tokens starting with a '>', we can't
just use a location pointing to the first '>' as the location of the split
token, because that would result in our miscomputing the length and spelling
for the token. As a consequence, for example, a refactoring replacing 'A<X>'
with something else would sometimes replace one character too many, and
similarly diagnostics highlighting a template-id source range would highlight
one character too many.
Fix this by creating an expansion range covering the first character of the
'>>' token, whose spelling is '>'. For this to work, we generalize the
expansion range of a macro FileID to be either a token range (the common case)
or a character range (used in this new case).
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@331155 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/clang/Lex')
-rw-r--r-- | include/clang/Lex/Lexer.h | 14 | ||||
-rw-r--r-- | include/clang/Lex/Preprocessor.h | 5 |
2 files changed, 17 insertions, 2 deletions
diff --git a/include/clang/Lex/Lexer.h b/include/clang/Lex/Lexer.h index d58849654c..15ef8300d0 100644 --- a/include/clang/Lex/Lexer.h +++ b/include/clang/Lex/Lexer.h @@ -336,13 +336,23 @@ public: const SourceManager &SM, const LangOptions &LangOpts); + /// Get the physical length (including trigraphs and escaped newlines) of the + /// first \p Characters characters of the token starting at TokStart. + static unsigned getTokenPrefixLength(SourceLocation TokStart, + unsigned Characters, + const SourceManager &SM, + const LangOptions &LangOpts); + /// AdvanceToTokenCharacter - If the current SourceLocation specifies a /// location at the start of a token, return a new location that specifies a /// character within the token. This handles trigraphs and escaped newlines. static SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart, - unsigned Character, + unsigned Characters, const SourceManager &SM, - const LangOptions &LangOpts); + const LangOptions &LangOpts) { + return TokStart.getLocWithOffset( + getTokenPrefixLength(TokStart, Characters, SM, LangOpts)); + } /// \brief Computes the source location just past the end of the /// token at this source location. diff --git a/include/clang/Lex/Preprocessor.h b/include/clang/Lex/Preprocessor.h index 630b87ced3..49d5da731a 100644 --- a/include/clang/Lex/Preprocessor.h +++ b/include/clang/Lex/Preprocessor.h @@ -1616,6 +1616,11 @@ public: SourceLocation ExpansionLocStart = SourceLocation(), SourceLocation ExpansionLocEnd = SourceLocation()); + /// Split the first Length characters out of the token starting at TokLoc + /// and return a location pointing to the split token. Re-lexing from the + /// split token will return the split token rather than the original. + SourceLocation SplitToken(SourceLocation TokLoc, unsigned Length); + /// \brief Computes the source location just past the end of the /// token at this source location. /// |