Skip to content

Commit 6495e35

Browse files
authored
Use native AddIncludeFile method (#18)
This PR changes the `add_source_file` method in Rust to use `AddIncludeFile` in C++ which resolves given file paths from include directories. This PR depends on #19. # References - mlir-rs/melior#654 - https://llvm.org/doxygen/classllvm_1_1SourceMgr.html
1 parent 2aa8fda commit 6495e35

File tree

4 files changed

+64
-44
lines changed

4 files changed

+64
-44
lines changed

cc/include/TableGen.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ extern "C" {
2626
#endif
2727

2828
typedef enum {
29-
TABLEGEN_DK_ERROR,
30-
TABLEGEN_DK_WARNING,
31-
TABLEGEN_DK_REMARK,
32-
TABLEGEN_DK_NOTE,
29+
TABLEGEN_DK_ERROR,
30+
TABLEGEN_DK_WARNING,
31+
TABLEGEN_DK_REMARK,
32+
TABLEGEN_DK_NOTE,
3333
} TableGenDiagKind;
3434

3535
typedef enum {
@@ -54,9 +54,8 @@ typedef void (*TableGenStringCallback)(TableGenStringRef, void *);
5454
TableGenParserRef tableGenGet();
5555
void tableGenFree(TableGenParserRef tg_ref);
5656
TableGenBool tableGenAddSource(TableGenParserRef tg_ref, const char *source);
57-
TableGenBool tableGenAddSourceFile(TableGenParserRef tg_ref,
58-
TableGenStringRef source);
59-
void tableGenAddIncludePath(TableGenParserRef tg_ref,
57+
void tableGenAddSourceFile(TableGenParserRef tg_ref, TableGenStringRef source);
58+
void tableGenAddIncludeDirectory(TableGenParserRef tg_ref,
6059
TableGenStringRef include);
6160

6261
/// NOTE: TableGen currently relies on global state within a given parser
@@ -162,14 +161,17 @@ TableGenBool tableGenIntInitGetValue(TableGenTypedInitRef ti, int64_t *integer);
162161
TableGenStringRef tableGenStringInitGetValue(TableGenTypedInitRef ti);
163162
char *tableGenStringInitGetValueNewString(TableGenTypedInitRef ti);
164163
TableGenRecordRef tableGenDefInitGetValue(TableGenTypedInitRef ti);
165-
void tableGenInitPrint(TableGenTypedInitRef ti,
166-
TableGenStringCallback callback, void *userData);
164+
void tableGenInitPrint(TableGenTypedInitRef ti, TableGenStringCallback callback,
165+
void *userData);
167166
void tableGenInitDump(TableGenTypedInitRef ti);
168-
TableGenBool tableGenPrintError(TableGenParserRef ref, TableGenSourceLocationRef loc_ref, TableGenDiagKind dk,
169-
TableGenStringRef message,
170-
TableGenStringCallback callback, void *userData);
167+
TableGenBool tableGenPrintError(TableGenParserRef ref,
168+
TableGenSourceLocationRef loc_ref,
169+
TableGenDiagKind dk, TableGenStringRef message,
170+
TableGenStringCallback callback,
171+
void *userData);
171172
TableGenSourceLocationRef tableGenSourceLocationNull();
172-
TableGenSourceLocationRef tableGenSourceLocationClone(TableGenSourceLocationRef loc_ref);
173+
TableGenSourceLocationRef
174+
tableGenSourceLocationClone(TableGenSourceLocationRef loc_ref);
173175

174176
// Memory
175177
void tableGenSourceLocationFree(TableGenSourceLocationRef loc_ref);

cc/lib/TableGen.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ using ctablegen::tableGenFromRecType;
1919
RecordKeeper *ctablegen::TableGenParser::parse() {
2020
auto recordKeeper = new RecordKeeper;
2121
sourceMgr.setIncludeDirs(includeDirs);
22+
23+
for (const auto &file : files) {
24+
std::string full_path;
25+
if (!sourceMgr.AddIncludeFile(file, SMLoc(), full_path)) {
26+
return nullptr;
27+
}
28+
}
29+
2230
bool result = TableGenParseFile(sourceMgr, *recordKeeper);
2331
if (!result) {
2432
return recordKeeper;
@@ -27,7 +35,7 @@ RecordKeeper *ctablegen::TableGenParser::parse() {
2735
return nullptr;
2836
}
2937

30-
void ctablegen::TableGenParser::addIncludePath(const StringRef include) {
38+
void ctablegen::TableGenParser::addIncludeDirectory(const StringRef include) {
3139
includeDirs.push_back(std::string(include));
3240
}
3341

@@ -43,16 +51,8 @@ bool ctablegen::TableGenParser::addSource(const char *source) {
4351
return true;
4452
}
4553

46-
bool ctablegen::TableGenParser::addSourceFile(const StringRef source) {
47-
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
48-
MemoryBuffer::getFile(source);
49-
50-
if (std::error_code EC = FileOrErr.getError()) {
51-
return false;
52-
}
53-
54-
sourceMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());
55-
return true;
54+
void ctablegen::TableGenParser::addSourceFile(const StringRef file) {
55+
files.push_back(std::string(file));
5656
}
5757

5858
TableGenParserRef tableGenGet() {
@@ -61,18 +61,18 @@ TableGenParserRef tableGenGet() {
6161

6262
void tableGenFree(TableGenParserRef tg_ref) { delete unwrap(tg_ref); }
6363

64-
TableGenBool tableGenAddSourceFile(TableGenParserRef tg_ref,
65-
TableGenStringRef source) {
66-
return unwrap(tg_ref)->addSourceFile(StringRef(source.data, source.len));
64+
void tableGenAddSourceFile(TableGenParserRef tg_ref, TableGenStringRef source) {
65+
unwrap(tg_ref)->addSourceFile(StringRef(source.data, source.len));
6766
}
6867

6968
TableGenBool tableGenAddSource(TableGenParserRef tg_ref, const char *source) {
7069
return unwrap(tg_ref)->addSource(source);
7170
}
7271

73-
void tableGenAddIncludePath(TableGenParserRef tg_ref,
72+
void tableGenAddIncludeDirectory(TableGenParserRef tg_ref,
7473
TableGenStringRef include) {
75-
return unwrap(tg_ref)->addIncludePath(StringRef(include.data, include.len));
74+
return unwrap(tg_ref)->addIncludeDirectory(
75+
StringRef(include.data, include.len));
7676
}
7777

7878
TableGenRecordKeeperRef tableGenParse(TableGenParserRef tg_ref) {

cc/lib/TableGen.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ class TableGenParser {
3838
public:
3939
TableGenParser() {}
4040
bool addSource(const char *source);
41-
bool addSourceFile(const StringRef source);
42-
void addIncludePath(const StringRef include);
41+
void addSourceFile(const StringRef source);
42+
void addIncludeDirectory(const StringRef include);
4343
RecordKeeper *parse();
4444

4545
SourceMgr sourceMgr;
46+
4647
private:
4748
std::vector<std::string> includeDirs;
49+
std::vector<std::string> files;
4850
};
4951

5052
// Utility
@@ -60,7 +62,7 @@ class CallbackOstream : public llvm::raw_ostream {
6062
opaqueData(opaqueData), pos(0u) {}
6163

6264
void write_impl(const char *ptr, size_t size) override {
63-
TableGenStringRef string = TableGenStringRef { .data = ptr, .len = size };
65+
TableGenStringRef string = TableGenStringRef{.data = ptr, .len = size};
6466
callback(string, opaqueData);
6567
pos += size;
6668
}
@@ -75,7 +77,8 @@ class CallbackOstream : public llvm::raw_ostream {
7577

7678
} // namespace ctablegen
7779

78-
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ctablegen::TableGenParser, TableGenParserRef);
80+
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ctablegen::TableGenParser,
81+
TableGenParserRef);
7982
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(RecordKeeper, TableGenRecordKeeperRef);
8083

8184
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ctablegen::RecordMap, TableGenRecordMapRef);

src/lib.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,25 @@
6262
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
6363
//! let keeper: RecordKeeper = TableGenParser::new()
6464
//! .add_source(r#"include "mlir/IR/OpBase.td""#)?
65-
//! .add_include_path(&format!("{}/include", std::env::var("TABLEGEN_190_PREFIX")?))
65+
//! .add_include_directory(&format!("{}/include", std::env::var("TABLEGEN_190_PREFIX")?))
66+
//! .parse()?;
67+
//! let i32_def = keeper.def("I32").expect("has I32 def");
68+
//! assert!(i32_def.subclass_of("I"));
69+
//! assert_eq!(i32_def.int_value("bitwidth"), Ok(32));
70+
//! # Ok(())
71+
//! # }
72+
//! ```
73+
//!
74+
//! You can also pass an included filename directly.
75+
//!
76+
//! ```rust
77+
//! use tblgen_alt::{TableGenParser, RecordKeeper};
78+
//! use std::path::Path;
79+
//!
80+
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
81+
//! let keeper: RecordKeeper = TableGenParser::new()
82+
//! .add_source_file("mlir/IR/OpBase.td")
83+
//! .add_include_directory(&format!("{}/include", std::env::var("TABLEGEN_190_PREFIX")?))
6684
//! .parse()?;
6785
//! let i32_def = keeper.def("I32").expect("has I32 def");
6886
//! assert!(i32_def.subclass_of("I"));
@@ -108,8 +126,8 @@ pub use record::RecordValue;
108126
pub use record_keeper::RecordKeeper;
109127

110128
use raw::{
111-
tableGenAddIncludePath, tableGenAddSource, tableGenAddSourceFile, tableGenFree, tableGenGet,
112-
tableGenParse, TableGenParserRef,
129+
tableGenAddIncludeDirectory, tableGenAddSource, tableGenAddSourceFile, tableGenFree,
130+
tableGenGet, tableGenParse, TableGenParserRef,
113131
};
114132
use string_ref::StringRef;
115133

@@ -144,18 +162,15 @@ impl<'s> TableGenParser<'s> {
144162
}
145163

146164
/// Adds the given path to the list of included directories.
147-
pub fn add_include_path(self, include: &str) -> Self {
148-
unsafe { tableGenAddIncludePath(self.raw, StringRef::from(include).to_raw()) }
165+
pub fn add_include_directory(self, include: &str) -> Self {
166+
unsafe { tableGenAddIncludeDirectory(self.raw, StringRef::from(include).to_raw()) }
149167
self
150168
}
151169

152170
/// Reads TableGen source code from the file at the given path.
153-
pub fn add_source_file(self, source: &str) -> Result<Self, Error> {
154-
if unsafe { tableGenAddSourceFile(self.raw, StringRef::from(source).to_raw()) > 0 } {
155-
Ok(self)
156-
} else {
157-
Err(TableGenError::InvalidSource.into())
158-
}
171+
pub fn add_source_file(self, source: &str) -> Self {
172+
unsafe { tableGenAddSourceFile(self.raw, StringRef::from(source).to_raw()) }
173+
self
159174
}
160175

161176
/// Adds the given TableGen source string.

0 commit comments

Comments
 (0)