Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 2dec5fc

Browse files
committed
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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@264884 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 21db7b7 commit 2dec5fc

File tree

7 files changed

+58
-0
lines changed

7 files changed

+58
-0
lines changed

lib/AsmParser/LLLexer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ lltok::Kind LLLexer::LexIdentifier() {
533533
KEYWORD(notail);
534534
KEYWORD(target);
535535
KEYWORD(triple);
536+
KEYWORD(source_filename);
536537
KEYWORD(unwind);
537538
KEYWORD(deplibs); // FIXME: Remove in 4.0.
538539
KEYWORD(datalayout);

lib/AsmParser/LLParser.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ bool LLParser::ParseTopLevelEntities() {
239239
case lltok::kw_define: if (ParseDefine()) return true; break;
240240
case lltok::kw_module: if (ParseModuleAsm()) return true; break;
241241
case lltok::kw_target: if (ParseTargetDefinition()) return true; break;
242+
case lltok::kw_source_filename:
243+
if (ParseSourceFileName())
244+
return true;
245+
break;
242246
case lltok::kw_deplibs: if (ParseDepLibs()) return true; break;
243247
case lltok::LocalVarID: if (ParseUnnamedType()) return true; break;
244248
case lltok::LocalVar: if (ParseNamedType()) return true; break;
@@ -335,6 +339,19 @@ bool LLParser::ParseTargetDefinition() {
335339
}
336340
}
337341

342+
/// toplevelentity
343+
/// ::= 'source_filename' '=' STRINGCONSTANT
344+
bool LLParser::ParseSourceFileName() {
345+
assert(Lex.getKind() == lltok::kw_source_filename);
346+
std::string Str;
347+
Lex.Lex();
348+
if (ParseToken(lltok::equal, "expected '=' after source_filename") ||
349+
ParseStringConstant(Str))
350+
return true;
351+
M->setSourceFileName(Str);
352+
return false;
353+
}
354+
338355
/// toplevelentity
339356
/// ::= 'deplibs' '=' '[' ']'
340357
/// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']'

lib/AsmParser/LLParser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ namespace llvm {
261261
bool ValidateEndOfModule();
262262
bool ParseTargetDefinition();
263263
bool ParseModuleAsm();
264+
bool ParseSourceFileName();
264265
bool ParseDepLibs(); // FIXME: Remove in 4.0.
265266
bool ParseUnnamedType();
266267
bool ParseNamedType();

lib/AsmParser/LLToken.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ namespace lltok {
5959
kw_notail,
6060
kw_target,
6161
kw_triple,
62+
kw_source_filename,
6263
kw_unwind,
6364
kw_deplibs, // FIXME: Remove in 4.0
6465
kw_datalayout,

lib/IR/AsmWriter.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,6 +2203,22 @@ void AssemblyWriter::writeOperandBundles(ImmutableCallSite CS) {
22032203
Out << " ]";
22042204
}
22052205

2206+
/// Escape any backslashes in the source file (e.g. Windows paths)
2207+
/// before emitting, so that it is parsed properly by the lexer on input.
2208+
static void EscapeBackslashes(std::string Str,
2209+
SmallVectorImpl<char> &Res) {
2210+
for (auto C : Str) {
2211+
switch (C) {
2212+
default:
2213+
break;
2214+
case '\\':
2215+
Res.push_back('\\');
2216+
break;
2217+
}
2218+
Res.push_back(C);
2219+
}
2220+
}
2221+
22062222
void AssemblyWriter::printModule(const Module *M) {
22072223
Machine.initialize();
22082224

@@ -2215,6 +2231,12 @@ void AssemblyWriter::printModule(const Module *M) {
22152231
M->getModuleIdentifier().find('\n') == std::string::npos)
22162232
Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
22172233

2234+
if (!M->getSourceFileName().empty()) {
2235+
SmallString<128> EscapedName;
2236+
EscapeBackslashes(M->getSourceFileName(), EscapedName);
2237+
Out << "source_filename = \"" << EscapedName << "\"\n";
2238+
}
2239+
22182240
const std::string &DL = M->getDataLayoutStr();
22192241
if (!DL.empty())
22202242
Out << "target datalayout = \"" << DL << "\"\n";
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
; Make sure that llvm-as/llvm-dis properly assemble/disassemble the
3+
; source_filename.
4+
5+
; RUN: llvm-as < %s | llvm-dis | FileCheck %s
6+
7+
; CHECK: source_filename = "C:\\path\\with\\backslashes\\test.cc"
8+
source_filename = "C:\\path\\with\\backslashes\\test.cc"

test/Assembler/source-filename.ll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
; Make sure that llvm-as/llvm-dis properly assemble/disassemble the
3+
; source_filename.
4+
5+
; RUN: llvm-as < %s | llvm-dis | FileCheck %s
6+
7+
; CHECK: source_filename = "test.cc"
8+
source_filename = "test.cc"

0 commit comments

Comments
 (0)