Skip to content

Commit 758c1f9

Browse files
[llvm-remarkutil] Auto-detect serializer format based on file extension
If serializer format is set to Auto, try to detect user intent based on the file extension of the output file name. Pull Request: llvm#160545
1 parent 6e6a3d8 commit 758c1f9

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

llvm/test/tools/llvm-remarkutil/filter.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,19 @@ RUN: llvm-remarkutil filter --remark-type=analysis %p/Inputs/filter.yaml | FileC
1818

1919
RUN: llvm-remarkutil yaml2bitstream -o %t.opt.bitstream %p/Inputs/filter.yaml
2020
RUN: llvm-remarkutil filter --function=func1 %t.opt.bitstream | FileCheck %s --strict-whitespace --check-prefix=REMARK1
21+
RUN: llvm-remarkutil filter --function=func1 %t.opt.bitstream -o %t.r1.yamL
22+
RUN: cat %t.r1.yamL | FileCheck %s --strict-whitespace --check-prefix=REMARK1
23+
RUN: llvm-remarkutil filter --function=func1 %t.opt.bitstream -o %t.r1.yMl
24+
RUN: cat %t.r1.yMl | FileCheck %s --strict-whitespace --check-prefix=REMARK1
25+
RUN: llvm-remarkutil filter --function=func1 %t.opt.bitstream --serializer=yaml -o %t.r1.fake.opt.bitstream
26+
RUN: cat %t.r1.fake.opt.bitstream | FileCheck %s --strict-whitespace --check-prefix=REMARK1
2127

2228
RUN: llvm-remarkutil filter --function=func1 %t.opt.bitstream -o %t.r1.opt.bitstream
2329
RUN: llvm-remarkutil bitstream2yaml %t.r1.opt.bitstream | FileCheck %s --strict-whitespace --check-prefix=REMARK1
30+
RUN: llvm-remarkutil filter --function=func1 %t.opt.bitstream -o %t.r1
31+
RUN: llvm-remarkutil bitstream2yaml %t.r1 | FileCheck %s --strict-whitespace --check-prefix=REMARK1
32+
RUN: llvm-remarkutil filter --function=func1 %p/Inputs/filter.yaml --serializer=bitstream -o %t.r1.fake.yaml
33+
RUN: llvm-remarkutil bitstream2yaml %t.r1.fake.yaml | FileCheck %s --strict-whitespace --check-prefix=REMARK1
2434

2535
RUN: llvm-remarkutil filter --function=func %p/Inputs/filter.yaml | FileCheck %s --allow-empty --strict-whitespace --check-prefix=EMPTY
2636

llvm/tools/llvm-remarkutil/RemarkFilter.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,8 @@ static Error tryFilter() {
4646
return MaybeParser.takeError();
4747
auto &Parser = **MaybeParser;
4848

49-
Format SerializerFormat = OutputFormat;
50-
if (SerializerFormat == Format::Auto) {
51-
SerializerFormat = Parser.ParserFormat;
52-
if (OutputFileName.empty() || OutputFileName == "-")
53-
SerializerFormat = Format::YAML;
54-
}
49+
Format SerializerFormat =
50+
getSerializerFormat(OutputFileName, OutputFormat, Parser.ParserFormat);
5551

5652
auto MaybeOF = getOutputFileForRemarks(OutputFileName, SerializerFormat);
5753
if (!MaybeOF)

llvm/tools/llvm-remarkutil/RemarkUtilHelpers.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ getOutputFileForRemarks(StringRef OutputFileName, Format OutputFormat) {
5454
: sys::fs::OF_None);
5555
}
5656

57+
Format getSerializerFormat(StringRef OutputFileName, Format SelectedFormat,
58+
Format DefaultFormat) {
59+
if (SelectedFormat != Format::Auto)
60+
return SelectedFormat;
61+
SelectedFormat = DefaultFormat;
62+
if (OutputFileName.empty() || OutputFileName == "-" ||
63+
OutputFileName.ends_with_insensitive(".yaml") ||
64+
OutputFileName.ends_with_insensitive(".yml"))
65+
SelectedFormat = Format::YAML;
66+
if (OutputFileName.ends_with_insensitive(".bitstream"))
67+
SelectedFormat = Format::Bitstream;
68+
return SelectedFormat;
69+
}
70+
5771
Expected<FilterMatcher>
5872
FilterMatcher::createRE(const llvm::cl::opt<std::string> &Arg) {
5973
return createRE(Arg.ArgStr, Arg);

llvm/tools/llvm-remarkutil/RemarkUtilHelpers.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"serializer", cl::init(Format::Auto), \
4848
cl::desc("Output remark format to serialize"), \
4949
cl::values(clEnumValN(Format::Auto, "auto", \
50-
"Follow the parser format (default)"), \
50+
"Automatic detection based on output file " \
51+
"extension or parser format (default)"), \
5152
clEnumValN(Format::YAML, "yaml", "YAML"), \
5253
clEnumValN(Format::Bitstream, "bitstream", "Bitstream")), \
5354
cl::sub(SUBOPT));
@@ -151,6 +152,12 @@ getOutputFileWithFlags(StringRef OutputFileName, sys::fs::OpenFlags Flags);
151152
Expected<std::unique_ptr<ToolOutputFile>>
152153
getOutputFileForRemarks(StringRef OutputFileName, Format OutputFormat);
153154

155+
/// Choose the serializer format. If \p SelectedFormat is Format::Auto, try to
156+
/// detect the format based on the extension of \p OutputFileName or fall back
157+
/// to \p DefaultFormat.
158+
Format getSerializerFormat(StringRef OutputFileName, Format SelectedFormat,
159+
Format DefaultFormat);
160+
154161
/// Filter object which can be either a string or a regex to match with the
155162
/// remark properties.
156163
class FilterMatcher {

0 commit comments

Comments
 (0)