Skip to content

Commit 518b6c9

Browse files
committed
[llvm-rc] Handle C preprocessor output
When preprocessing resource scripts (which can easily be done outside of llvm-rc), included headers can leave behind C declarations (despite preprocessing with -DRC_INVOKED), that can't be parsed by a resource compiler. This is handled in all of rc.exe, by parsing the preprocessor output line markers and ignoring content from files named *.h and *.c, documented at [1]. In addition to this filtering, strip out any other preprocessor directive that is left behind (like pragmas) which also can't be handled by the tokenizer. The added test uses both standard #line markers (supported by rc.exe) and GNU style extended line markers, thus this test doesn't pass with rc.exe, but passes with GNU windres. (Windres on the other hand doesn't filter out files named *.c, only *.h.) Differential Revision: https://reviews.llvm.org/D46579 [1] https://msdn.microsoft.com/en-us/library/windows/desktop/aa381033(v=vs.85).aspx llvm-svn: 331903
1 parent 7bc3c58 commit 518b6c9

File tree

6 files changed

+196
-1
lines changed

6 files changed

+196
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Test that the input originally is included.
2+
STRINGTABLE {
3+
1 "a"
4+
}
5+
#line 2 "cpp-source.rc"
6+
// Content from a rc file (potentially the source file itself) is included.
7+
STRINGTABLE {
8+
2 "b"
9+
}
10+
// Test a preprocessing directive that starts with leading whitespace.
11+
#line 1 "\\some\\path\\header.h"
12+
// Content from .h files is ignored.
13+
typedef int Foo;
14+
#line 123 "\\some\\path\\header.h"
15+
void someFunc(void);
16+
// Check GNU style line markers.
17+
# 4 "cpp-source.rc" 1
18+
STRINGTABLE {
19+
3 "c"
20+
}
21+
# 1 "other/header.h" 1
22+
typedef int Bar;
23+
# 10 "cpp-source.rc" 2
24+
// Test that other preprocessor directives are ignored.
25+
#pragma foo
26+
STRINGTABLE {
27+
4 "d"
28+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; RUN: llvm-rc /FO %t %p/Inputs/cpp-output.rc
2+
; RUN: llvm-readobj %t | FileCheck %s
3+
4+
; CHECK: Resource type (int): 6
5+
; CHECK-NEXT: Resource name (int): 1
6+
; CHECK-NEXT: Data version: 0
7+
; CHECK-NEXT: Memory flags: 0x1030
8+
; CHECK-NEXT: Language ID: 1033
9+
; CHECK-NEXT: Version (major): 0
10+
; CHECK-NEXT: Version (minor): 0
11+
; CHECK-NEXT: Characteristics: 0
12+
; CHECK-NEXT: Data size: 40
13+
; CHECK-NEXT: Data: (
14+
; CHECK-NEXT: 0000: 00000100 61000100 62000100 63000100 |....a...b...c...|
15+
; CHECK-NEXT: 0010: 64000000 00000000 00000000 00000000 |d...............|
16+
; CHECK-NEXT: 0020: 00000000 00000000 |........|
17+
; CHECK-NEXT: )

llvm/tools/llvm-rc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add_public_tablegen_target(RcTableGen)
1111
add_llvm_tool(llvm-rc
1212
llvm-rc.cpp
1313
ResourceFileWriter.cpp
14+
ResourceScriptCppFilter.cpp
1415
ResourceScriptParser.cpp
1516
ResourceScriptStmt.cpp
1617
ResourceScriptToken.cpp
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
//===-- ResourceScriptCppFilter.cpp ----------------------------*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===---------------------------------------------------------------------===//
9+
//
10+
// This file implements an interface defined in ResourceScriptCppFilter.h.
11+
//
12+
//===---------------------------------------------------------------------===//
13+
14+
#include "ResourceScriptCppFilter.h"
15+
#include "llvm/ADT/StringExtras.h"
16+
17+
#include <vector>
18+
19+
using namespace llvm;
20+
21+
namespace {
22+
23+
class Filter {
24+
public:
25+
explicit Filter(StringRef Input) : Data(Input), DataLength(Input.size()) {}
26+
27+
std::string run();
28+
29+
private:
30+
// Parse the line, returning whether the line should be included in
31+
// the output.
32+
bool parseLine(StringRef Line);
33+
34+
bool streamEof() const;
35+
36+
StringRef Data;
37+
size_t DataLength;
38+
39+
size_t Pos = 0;
40+
bool Outputting = true;
41+
};
42+
43+
std::string Filter::run() {
44+
std::vector<StringRef> Output;
45+
46+
while (!streamEof() && Pos != StringRef::npos) {
47+
size_t LineStart = Pos;
48+
Pos = Data.find_first_of("\r\n", Pos);
49+
Pos = Data.find_first_not_of("\r\n", Pos);
50+
StringRef Line = Data.take_front(Pos).drop_front(LineStart);
51+
52+
if (parseLine(Line))
53+
Output.push_back(Line);
54+
}
55+
56+
return llvm::join(Output, "");
57+
}
58+
59+
bool Filter::parseLine(StringRef Line) {
60+
Line = Line.ltrim();
61+
62+
if (!Line.consume_front("#")) {
63+
// A normal content line, filtered according to the current mode.
64+
return Outputting;
65+
}
66+
67+
// Found a preprocessing directive line. From here on, we always return
68+
// false since the preprocessing directives should be filtered out.
69+
70+
Line.consume_front("line");
71+
if (!Line.startswith(" "))
72+
return false; // Not a line directive (pragma etc).
73+
74+
// #line 123 "path/file.h"
75+
// # 123 "path/file.h" 1
76+
77+
Line =
78+
Line.ltrim(); // There could be multiple spaces after the #line directive
79+
80+
size_t N;
81+
if (Line.consumeInteger(10, N)) // Returns true to signify an error
82+
return false;
83+
84+
Line = Line.ltrim();
85+
86+
if (!Line.consume_front("\""))
87+
return false; // Malformed line, no quote found.
88+
89+
// Split the string at the last quote (in case the path name had
90+
// escaped quotes as well).
91+
Line = Line.rsplit('"').first;
92+
93+
StringRef Ext = Line.rsplit('.').second;
94+
95+
if (Ext.equals_lower("h") || Ext.equals_lower("c")) {
96+
Outputting = false;
97+
} else {
98+
Outputting = true;
99+
}
100+
101+
return false;
102+
}
103+
104+
bool Filter::streamEof() const { return Pos == DataLength; }
105+
106+
} // anonymous namespace
107+
108+
namespace llvm {
109+
110+
std::string filterCppOutput(StringRef Input) { return Filter(Input).run(); }
111+
112+
} // namespace llvm
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===-- ResourceScriptCppFilter.h ------------------------------*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===---------------------------------------------------------------------===//
9+
//
10+
// This filters the input to llvm-rc for preprocessor markers, removing
11+
// preprocessing directives that a preprocessor can output or leave behind.
12+
//
13+
// It also filters out any contribution from files named *.h or *.c, based
14+
// on preprocessor line markers. When preprocessing RC files, the included
15+
// headers can leave behind C declarations, that RC doesn't understand.
16+
// Rc.exe simply discards anything that comes from files named *.h or *.h.
17+
//
18+
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa381033(v=vs.85).aspx
19+
//
20+
//===---------------------------------------------------------------------===//
21+
22+
#ifndef LLVM_TOOLS_LLVMRC_RESOURCESCRIPTCPPFILTER_H
23+
#define LLVM_TOOLS_LLVMRC_RESOURCESCRIPTCPPFILTER_H
24+
25+
#include "llvm/ADT/StringRef.h"
26+
27+
#include <string>
28+
29+
namespace llvm {
30+
31+
std::string filterCppOutput(StringRef Input);
32+
33+
} // namespace llvm
34+
35+
#endif

llvm/tools/llvm-rc/llvm-rc.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
#include "ResourceFileWriter.h"
16+
#include "ResourceScriptCppFilter.h"
1617
#include "ResourceScriptParser.h"
1718
#include "ResourceScriptStmt.h"
1819
#include "ResourceScriptToken.h"
@@ -111,7 +112,8 @@ int main(int Argc, const char **Argv) {
111112
std::unique_ptr<MemoryBuffer> FileContents = std::move(*File);
112113
StringRef Contents = FileContents->getBuffer();
113114

114-
std::vector<RCToken> Tokens = ExitOnErr(tokenizeRC(Contents));
115+
std::string FilteredContents = filterCppOutput(Contents);
116+
std::vector<RCToken> Tokens = ExitOnErr(tokenizeRC(FilteredContents));
115117

116118
if (BeVerbose) {
117119
const Twine TokenNames[] = {

0 commit comments

Comments
 (0)