Skip to content

Commit

Permalink
Use native AddIncludeFile method (#18)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
raviqqe authored Jan 7, 2025
1 parent 2aa8fda commit 6495e35
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 44 deletions.
28 changes: 15 additions & 13 deletions cc/include/TableGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ extern "C" {
#endif

typedef enum {
TABLEGEN_DK_ERROR,
TABLEGEN_DK_WARNING,
TABLEGEN_DK_REMARK,
TABLEGEN_DK_NOTE,
TABLEGEN_DK_ERROR,
TABLEGEN_DK_WARNING,
TABLEGEN_DK_REMARK,
TABLEGEN_DK_NOTE,
} TableGenDiagKind;

typedef enum {
Expand All @@ -54,9 +54,8 @@ typedef void (*TableGenStringCallback)(TableGenStringRef, void *);
TableGenParserRef tableGenGet();
void tableGenFree(TableGenParserRef tg_ref);
TableGenBool tableGenAddSource(TableGenParserRef tg_ref, const char *source);
TableGenBool tableGenAddSourceFile(TableGenParserRef tg_ref,
TableGenStringRef source);
void tableGenAddIncludePath(TableGenParserRef tg_ref,
void tableGenAddSourceFile(TableGenParserRef tg_ref, TableGenStringRef source);
void tableGenAddIncludeDirectory(TableGenParserRef tg_ref,
TableGenStringRef include);

/// NOTE: TableGen currently relies on global state within a given parser
Expand Down Expand Up @@ -162,14 +161,17 @@ TableGenBool tableGenIntInitGetValue(TableGenTypedInitRef ti, int64_t *integer);
TableGenStringRef tableGenStringInitGetValue(TableGenTypedInitRef ti);
char *tableGenStringInitGetValueNewString(TableGenTypedInitRef ti);
TableGenRecordRef tableGenDefInitGetValue(TableGenTypedInitRef ti);
void tableGenInitPrint(TableGenTypedInitRef ti,
TableGenStringCallback callback, void *userData);
void tableGenInitPrint(TableGenTypedInitRef ti, TableGenStringCallback callback,
void *userData);
void tableGenInitDump(TableGenTypedInitRef ti);
TableGenBool tableGenPrintError(TableGenParserRef ref, TableGenSourceLocationRef loc_ref, TableGenDiagKind dk,
TableGenStringRef message,
TableGenStringCallback callback, void *userData);
TableGenBool tableGenPrintError(TableGenParserRef ref,
TableGenSourceLocationRef loc_ref,
TableGenDiagKind dk, TableGenStringRef message,
TableGenStringCallback callback,
void *userData);
TableGenSourceLocationRef tableGenSourceLocationNull();
TableGenSourceLocationRef tableGenSourceLocationClone(TableGenSourceLocationRef loc_ref);
TableGenSourceLocationRef
tableGenSourceLocationClone(TableGenSourceLocationRef loc_ref);

// Memory
void tableGenSourceLocationFree(TableGenSourceLocationRef loc_ref);
Expand Down
32 changes: 16 additions & 16 deletions cc/lib/TableGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ using ctablegen::tableGenFromRecType;
RecordKeeper *ctablegen::TableGenParser::parse() {
auto recordKeeper = new RecordKeeper;
sourceMgr.setIncludeDirs(includeDirs);

for (const auto &file : files) {
std::string full_path;
if (!sourceMgr.AddIncludeFile(file, SMLoc(), full_path)) {
return nullptr;
}
}

bool result = TableGenParseFile(sourceMgr, *recordKeeper);
if (!result) {
return recordKeeper;
Expand All @@ -27,7 +35,7 @@ RecordKeeper *ctablegen::TableGenParser::parse() {
return nullptr;
}

void ctablegen::TableGenParser::addIncludePath(const StringRef include) {
void ctablegen::TableGenParser::addIncludeDirectory(const StringRef include) {
includeDirs.push_back(std::string(include));
}

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

bool ctablegen::TableGenParser::addSourceFile(const StringRef source) {
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
MemoryBuffer::getFile(source);

if (std::error_code EC = FileOrErr.getError()) {
return false;
}

sourceMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());
return true;
void ctablegen::TableGenParser::addSourceFile(const StringRef file) {
files.push_back(std::string(file));
}

TableGenParserRef tableGenGet() {
Expand All @@ -61,18 +61,18 @@ TableGenParserRef tableGenGet() {

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

TableGenBool tableGenAddSourceFile(TableGenParserRef tg_ref,
TableGenStringRef source) {
return unwrap(tg_ref)->addSourceFile(StringRef(source.data, source.len));
void tableGenAddSourceFile(TableGenParserRef tg_ref, TableGenStringRef source) {
unwrap(tg_ref)->addSourceFile(StringRef(source.data, source.len));
}

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

void tableGenAddIncludePath(TableGenParserRef tg_ref,
void tableGenAddIncludeDirectory(TableGenParserRef tg_ref,
TableGenStringRef include) {
return unwrap(tg_ref)->addIncludePath(StringRef(include.data, include.len));
return unwrap(tg_ref)->addIncludeDirectory(
StringRef(include.data, include.len));
}

TableGenRecordKeeperRef tableGenParse(TableGenParserRef tg_ref) {
Expand Down
11 changes: 7 additions & 4 deletions cc/lib/TableGen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ class TableGenParser {
public:
TableGenParser() {}
bool addSource(const char *source);
bool addSourceFile(const StringRef source);
void addIncludePath(const StringRef include);
void addSourceFile(const StringRef source);
void addIncludeDirectory(const StringRef include);
RecordKeeper *parse();

SourceMgr sourceMgr;

private:
std::vector<std::string> includeDirs;
std::vector<std::string> files;
};

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

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

} // namespace ctablegen

DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ctablegen::TableGenParser, TableGenParserRef);
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ctablegen::TableGenParser,
TableGenParserRef);
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(RecordKeeper, TableGenRecordKeeperRef);

DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ctablegen::RecordMap, TableGenRecordMapRef);
Expand Down
37 changes: 26 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,25 @@
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let keeper: RecordKeeper = TableGenParser::new()
//! .add_source(r#"include "mlir/IR/OpBase.td""#)?
//! .add_include_path(&format!("{}/include", std::env::var("TABLEGEN_190_PREFIX")?))
//! .add_include_directory(&format!("{}/include", std::env::var("TABLEGEN_190_PREFIX")?))
//! .parse()?;
//! let i32_def = keeper.def("I32").expect("has I32 def");
//! assert!(i32_def.subclass_of("I"));
//! assert_eq!(i32_def.int_value("bitwidth"), Ok(32));
//! # Ok(())
//! # }
//! ```
//!
//! You can also pass an included filename directly.
//!
//! ```rust
//! use tblgen_alt::{TableGenParser, RecordKeeper};
//! use std::path::Path;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let keeper: RecordKeeper = TableGenParser::new()
//! .add_source_file("mlir/IR/OpBase.td")
//! .add_include_directory(&format!("{}/include", std::env::var("TABLEGEN_190_PREFIX")?))
//! .parse()?;
//! let i32_def = keeper.def("I32").expect("has I32 def");
//! assert!(i32_def.subclass_of("I"));
Expand Down Expand Up @@ -108,8 +126,8 @@ pub use record::RecordValue;
pub use record_keeper::RecordKeeper;

use raw::{
tableGenAddIncludePath, tableGenAddSource, tableGenAddSourceFile, tableGenFree, tableGenGet,
tableGenParse, TableGenParserRef,
tableGenAddIncludeDirectory, tableGenAddSource, tableGenAddSourceFile, tableGenFree,
tableGenGet, tableGenParse, TableGenParserRef,
};
use string_ref::StringRef;

Expand Down Expand Up @@ -144,18 +162,15 @@ impl<'s> TableGenParser<'s> {
}

/// Adds the given path to the list of included directories.
pub fn add_include_path(self, include: &str) -> Self {
unsafe { tableGenAddIncludePath(self.raw, StringRef::from(include).to_raw()) }
pub fn add_include_directory(self, include: &str) -> Self {
unsafe { tableGenAddIncludeDirectory(self.raw, StringRef::from(include).to_raw()) }
self
}

/// Reads TableGen source code from the file at the given path.
pub fn add_source_file(self, source: &str) -> Result<Self, Error> {
if unsafe { tableGenAddSourceFile(self.raw, StringRef::from(source).to_raw()) > 0 } {
Ok(self)
} else {
Err(TableGenError::InvalidSource.into())
}
pub fn add_source_file(self, source: &str) -> Self {
unsafe { tableGenAddSourceFile(self.raw, StringRef::from(source).to_raw()) }
self
}

/// Adds the given TableGen source string.
Expand Down

0 comments on commit 6495e35

Please sign in to comment.