From 83c517c44eb3087b284ee73c49decb7aaf38e273 Mon Sep 17 00:00:00 2001 From: Teresa Johnson Date: Wed, 30 Mar 2016 18:15:08 +0000 Subject: [PATCH] Restore "[ThinLTO] Serialize the Module SourceFileName to/from LLVM assembly" This restores commit 264869, with a fix for windows bots to properly escape '\' in the path when serializing out. Added test. llvm-svn: 264884 --- llvm/lib/AsmParser/LLLexer.cpp | 1 + llvm/lib/AsmParser/LLParser.cpp | 17 +++++++++++++++++ llvm/lib/AsmParser/LLParser.h | 1 + llvm/lib/AsmParser/LLToken.h | 1 + llvm/lib/IR/AsmWriter.cpp | 22 ++++++++++++++++++++++ llvm/test/Assembler/source-filename-backslash.ll | 8 ++++++++ llvm/test/Assembler/source-filename.ll | 8 ++++++++ 7 files changed, 58 insertions(+) create mode 100644 llvm/test/Assembler/source-filename-backslash.ll create mode 100644 llvm/test/Assembler/source-filename.ll diff --git a/llvm/lib/AsmParser/LLLexer.cpp b/llvm/lib/AsmParser/LLLexer.cpp index d238513..46ffc64 100644 --- a/llvm/lib/AsmParser/LLLexer.cpp +++ b/llvm/lib/AsmParser/LLLexer.cpp @@ -533,6 +533,7 @@ lltok::Kind LLLexer::LexIdentifier() { KEYWORD(notail); KEYWORD(target); KEYWORD(triple); + KEYWORD(source_filename); KEYWORD(unwind); KEYWORD(deplibs); // FIXME: Remove in 4.0. KEYWORD(datalayout); diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp index 76bd99e..39c6135 100644 --- a/llvm/lib/AsmParser/LLParser.cpp +++ b/llvm/lib/AsmParser/LLParser.cpp @@ -239,6 +239,10 @@ bool LLParser::ParseTopLevelEntities() { case lltok::kw_define: if (ParseDefine()) return true; break; case lltok::kw_module: if (ParseModuleAsm()) return true; break; case lltok::kw_target: if (ParseTargetDefinition()) return true; break; + case lltok::kw_source_filename: + if (ParseSourceFileName()) + return true; + break; case lltok::kw_deplibs: if (ParseDepLibs()) return true; break; case lltok::LocalVarID: if (ParseUnnamedType()) return true; break; case lltok::LocalVar: if (ParseNamedType()) return true; break; @@ -336,6 +340,19 @@ bool LLParser::ParseTargetDefinition() { } /// toplevelentity +/// ::= 'source_filename' '=' STRINGCONSTANT +bool LLParser::ParseSourceFileName() { + assert(Lex.getKind() == lltok::kw_source_filename); + std::string Str; + Lex.Lex(); + if (ParseToken(lltok::equal, "expected '=' after source_filename") || + ParseStringConstant(Str)) + return true; + M->setSourceFileName(Str); + return false; +} + +/// toplevelentity /// ::= 'deplibs' '=' '[' ']' /// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']' /// FIXME: Remove in 4.0. Currently parse, but ignore. diff --git a/llvm/lib/AsmParser/LLParser.h b/llvm/lib/AsmParser/LLParser.h index 96f864a..fcece62 100644 --- a/llvm/lib/AsmParser/LLParser.h +++ b/llvm/lib/AsmParser/LLParser.h @@ -261,6 +261,7 @@ namespace llvm { bool ValidateEndOfModule(); bool ParseTargetDefinition(); bool ParseModuleAsm(); + bool ParseSourceFileName(); bool ParseDepLibs(); // FIXME: Remove in 4.0. bool ParseUnnamedType(); bool ParseNamedType(); diff --git a/llvm/lib/AsmParser/LLToken.h b/llvm/lib/AsmParser/LLToken.h index f06b5b8..c5a7490 100644 --- a/llvm/lib/AsmParser/LLToken.h +++ b/llvm/lib/AsmParser/LLToken.h @@ -59,6 +59,7 @@ namespace lltok { kw_notail, kw_target, kw_triple, + kw_source_filename, kw_unwind, kw_deplibs, // FIXME: Remove in 4.0 kw_datalayout, diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp index ab56f080..4750cf6 100644 --- a/llvm/lib/IR/AsmWriter.cpp +++ b/llvm/lib/IR/AsmWriter.cpp @@ -2203,6 +2203,22 @@ void AssemblyWriter::writeOperandBundles(ImmutableCallSite CS) { Out << " ]"; } +/// Escape any backslashes in the source file (e.g. Windows paths) +/// before emitting, so that it is parsed properly by the lexer on input. +static void EscapeBackslashes(std::string Str, + SmallVectorImpl &Res) { + for (auto C : Str) { + switch (C) { + default: + break; + case '\\': + Res.push_back('\\'); + break; + } + Res.push_back(C); + } +} + void AssemblyWriter::printModule(const Module *M) { Machine.initialize(); @@ -2215,6 +2231,12 @@ void AssemblyWriter::printModule(const Module *M) { M->getModuleIdentifier().find('\n') == std::string::npos) Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n"; + if (!M->getSourceFileName().empty()) { + SmallString<128> EscapedName; + EscapeBackslashes(M->getSourceFileName(), EscapedName); + Out << "source_filename = \"" << EscapedName << "\"\n"; + } + const std::string &DL = M->getDataLayoutStr(); if (!DL.empty()) Out << "target datalayout = \"" << DL << "\"\n"; diff --git a/llvm/test/Assembler/source-filename-backslash.ll b/llvm/test/Assembler/source-filename-backslash.ll new file mode 100644 index 0000000..27b5198 --- /dev/null +++ b/llvm/test/Assembler/source-filename-backslash.ll @@ -0,0 +1,8 @@ + +; Make sure that llvm-as/llvm-dis properly assemble/disassemble the +; source_filename. + +; RUN: llvm-as < %s | llvm-dis | FileCheck %s + +; CHECK: source_filename = "C:\\path\\with\\backslashes\\test.cc" +source_filename = "C:\\path\\with\\backslashes\\test.cc" diff --git a/llvm/test/Assembler/source-filename.ll b/llvm/test/Assembler/source-filename.ll new file mode 100644 index 0000000..11284fa --- /dev/null +++ b/llvm/test/Assembler/source-filename.ll @@ -0,0 +1,8 @@ + +; Make sure that llvm-as/llvm-dis properly assemble/disassemble the +; source_filename. + +; RUN: llvm-as < %s | llvm-dis | FileCheck %s + +; CHECK: source_filename = "test.cc" +source_filename = "test.cc" -- 2.7.4