From 7f3b28a7867a35ec56a4ed80546e64995d69483e Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 13 Mar 2009 21:33:17 +0000 Subject: [PATCH] Fix escaping in asm string literals correctly by having tblgen unescape them, then the asmprinter emitter reescape them. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@66958 91177308-0d34-0410-b5e6-96231b3b80d8 --- utils/TableGen/AsmWriterEmitter.cpp | 19 ++++++++++++++----- utils/TableGen/TGLexer.cpp | 4 ++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/utils/TableGen/AsmWriterEmitter.cpp b/utils/TableGen/AsmWriterEmitter.cpp index e7fe8c369bd..93ef6c86a6b 100644 --- a/utils/TableGen/AsmWriterEmitter.cpp +++ b/utils/TableGen/AsmWriterEmitter.cpp @@ -130,11 +130,20 @@ AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant) { // Emit a constant string fragment. if (DollarPos != LastEmitted) { - // TODO: this should eventually handle escaping. - if (CurVariant == Variant || CurVariant == ~0U) - AddLiteralString(std::string(AsmString.begin()+LastEmitted, - AsmString.begin()+DollarPos)); - LastEmitted = DollarPos; + if (CurVariant == Variant || CurVariant == ~0U) { + for (; LastEmitted != DollarPos; ++LastEmitted) + switch (AsmString[LastEmitted]) { + case '\n': AddLiteralString("\\n"); break; + case '\t': AddLiteralString("\\t"); break; + case '"': AddLiteralString("\\\""); break; + case '\\': AddLiteralString("\\\\"); break; + default: + AddLiteralString(std::string(1, AsmString[LastEmitted])); + break; + } + } else { + LastEmitted = DollarPos; + } } else if (AsmString[DollarPos] == '\\') { if (DollarPos+1 != AsmString.size() && (CurVariant == Variant || CurVariant == ~0U)) { diff --git a/utils/TableGen/TGLexer.cpp b/utils/TableGen/TGLexer.cpp index 03bf0ceaa6b..79982aa05f5 100644 --- a/utils/TableGen/TGLexer.cpp +++ b/utils/TableGen/TGLexer.cpp @@ -174,11 +174,11 @@ tgtok::TokKind TGLexer::LexString() { CurStrVal += *CurPtr++; break; case 't': - CurStrVal += "\\t"; + CurStrVal += '\t'; ++CurPtr; break; case 'n': - CurStrVal += "\\n"; + CurStrVal += '\n'; ++CurPtr; break;