a metric ton of refactoring later, Sema::getLocationOfStringLiteralByte
no longer depends on Preprocessor, so we can move it out of Sema into
a nice new StringLiteral::getLocationOfByte method that can be used by
any AST client.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@119481 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp
index 0fa1fc1..d04addd 100644
--- a/lib/Sema/SemaChecking.cpp
+++ b/lib/Sema/SemaChecking.cpp
@@ -25,7 +25,6 @@
#include "clang/AST/DeclObjC.h"
#include "clang/AST/StmtCXX.h"
#include "clang/AST/StmtObjC.h"
-#include "clang/Lex/LiteralSupport.h"
#include "clang/Lex/Preprocessor.h"
#include "llvm/ADT/BitVector.h"
#include "llvm/ADT/STLExtras.h"
@@ -33,75 +32,16 @@
#include "clang/Basic/TargetBuiltins.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Basic/ConvertUTF.h"
-
#include <limits>
using namespace clang;
using namespace sema;
-/// getLocationOfStringLiteralByte - Return a source location that points to the
-/// specified byte of the specified string literal.
-///
-/// Strings are amazingly complex. They can be formed from multiple tokens and
-/// can have escape sequences in them in addition to the usual trigraph and
-/// escaped newline business. This routine handles this complexity.
-///
SourceLocation Sema::getLocationOfStringLiteralByte(const StringLiteral *SL,
unsigned ByteNo) const {
- assert(!SL->isWide() && "This doesn't work for wide strings yet");
-
- // Loop over all of the tokens in this string until we find the one that
- // contains the byte we're looking for.
- unsigned TokNo = 0;
- while (1) {
- assert(TokNo < SL->getNumConcatenated() && "Invalid byte number!");
- SourceLocation StrTokLoc = SL->getStrTokenLoc(TokNo);
-
- // Get the spelling of the string so that we can get the data that makes up
- // the string literal, not the identifier for the macro it is potentially
- // expanded through.
- SourceLocation StrTokSpellingLoc = SourceMgr.getSpellingLoc(StrTokLoc);
-
- // Re-lex the token to get its length and original spelling.
- std::pair<FileID, unsigned> LocInfo =
- SourceMgr.getDecomposedLoc(StrTokSpellingLoc);
- bool Invalid = false;
- llvm::StringRef Buffer = SourceMgr.getBufferData(LocInfo.first, &Invalid);
- if (Invalid)
- return StrTokSpellingLoc;
-
- const char *StrData = Buffer.data()+LocInfo.second;
-
- // Create a langops struct and enable trigraphs. This is sufficient for
- // relexing tokens.
- LangOptions LangOpts;
- LangOpts.Trigraphs = true;
-
- // Create a lexer starting at the beginning of this token.
- Lexer TheLexer(StrTokSpellingLoc, LangOpts, Buffer.begin(), StrData,
- Buffer.end());
- Token TheTok;
- TheLexer.LexFromRawLexer(TheTok);
-
- // Use the StringLiteralParser to compute the length of the string in bytes.
- StringLiteralParser SLP(&TheTok, 1, PP.getSourceManager(),
- PP.getLangOptions(), PP.getTargetInfo());
- unsigned TokNumBytes = SLP.GetStringLength();
-
- // If the byte is in this token, return the location of the byte.
- if (ByteNo < TokNumBytes ||
- (ByteNo == TokNumBytes && TokNo == SL->getNumConcatenated())) {
- unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
-
- // Now that we know the offset of the token in the spelling, use the
- // preprocessor to get the offset in the original source.
- return PP.AdvanceToTokenCharacter(StrTokLoc, Offset);
- }
-
- // Move to the next string token.
- ++TokNo;
- ByteNo -= TokNumBytes;
- }
+ return SL->getLocationOfByte(ByteNo, PP.getSourceManager(),
+ PP.getLangOptions(), PP.getTargetInfo());
}
+
/// CheckablePrintfAttr - does a function call have a "printf" attribute
/// and arguments that merit checking?