Adding a rewrite class to Clang

Hi

My goal is to create a source to source translator that translates Objective-C header files into D files. From what I've read here in this list I should create a rewrite class, something like RewriteObjC.cpp.

I started by modifying RewriteObjC.cpp but then thought that it would be better to add a new rewrite class. I modified the source code for Clang and added a new file called RewriteObjCToD.cpp by copying the existing RewriteObjC.cpp then I added a new command line option to Clang to be able to use the new rewrite class. After compiling everything I tried to run Clang using "clang --help" and got this error:

<FlagClass Name:"-rewrite-objc">
<FlagClass Name:"-rewrite-objc-to-d">
Assertion failed: (0 && "Options are not in order!"), function OptTable, file OptTable.cpp, line 119.
0 clang 0x00000001013764c2 PrintStackTrace(void*) + 34
1 clang 0x0000000101377163 SignalHandler(int) + 707
2 libSystem.B.dylib 0x000000010206435a _sigtramp + 26
3 libSystem.B.dylib 0x0000000102400000 _sigtramp + 3783872
4 clang 0x000000010001e9d2 __assert_rtn + 66
5 clang 0x00000001000aebec clang::driver::OptTable::OptTable(clang::driver::OptTable::Info const*, unsigned int) + 780
6 clang 0x00000001000a8a7a clang::driver::createDriverOptTable() + 42
7 clang 0x000000010009f6ef clang::driver::Driver::Driver(llvm::StringRef, llvm::StringRef, llvm::StringRef, bool, bool, clang::Diagnostic&) + 111
8 clang 0x0000000100025e90 main + 1360
9 clang 0x000000010001eca8 start + 52
Stack dump:
0. Program arguments: ./clang --help
Illegal instruction

So how do I correctly add a new command line option to Clang or is there a better way to quickly get up and running with a new rewrite class?

Hi Jacob!

Jacob Carlborg meinte am 12.09.2010 22:30:

<FlagClass Name:"-rewrite-objc">
<FlagClass Name:"-rewrite-objc-to-d">
Assertion failed: (0 && "Options are not in order!"), function OptTable,
file OptTable.cpp, line 119.

[...]

So how do I correctly add a new command line option to Clang

You just have to order them correctly [1]:

Options are stored in sorted order, with '\0' at the end of the
alphabet.o

So '-rewrite-objc-to-d' has to come *before* '-rewrite-objc'. The
ordering is necessary due to the way Clang parses its options.

or is there a better way to quickly get up and running with a new
rewrite class?

You could implement it as a plugin. There's an example in
'example/PrintFunctionNames'. Unfortunately the plugin infrastructure is
somewhat buggy/incomplete/?, so you have to add a minor change to llvm
before they work [2].

[1] http://clang.llvm.org/doxygen/OptTable_8cpp_source.html#l00201
[2] http://permalink.gmane.org/gmane.comp.compilers.clang.devel/8277

Regards, Jan.

Hi Jacob!

Jacob Carlborg meinte am 12.09.2010 22:30:

<FlagClass Name:"-rewrite-objc">
<FlagClass Name:"-rewrite-objc-to-d">
Assertion failed: (0 && "Options are not in order!"), function OptTable,
file OptTable.cpp, line 119.

[...]

So how do I correctly add a new command line option to Clang

You just have to order them correctly [1]:

> Options are stored in sorted order, with '\0' at the end of the
> alphabet.o

So '-rewrite-objc-to-d' has to come *before* '-rewrite-objc'. The
ordering is necessary due to the way Clang parses its options.

Note that this is no longer true, the option parser tblgen backend
handles the reordering now.

- Daniel

Hi Jacob!

Jacob Carlborg meinte am 12.09.2010 22:30:

<FlagClass Name:"-rewrite-objc">
<FlagClass Name:"-rewrite-objc-to-d">
Assertion failed: (0&& "Options are not in order!"), function OptTable,
file OptTable.cpp, line 119.

[...]

So how do I correctly add a new command line option to Clang

You just have to order them correctly [1]:

> Options are stored in sorted order, with '\0' at the end of the
> alphabet.o

So '-rewrite-objc-to-d' has to come *before* '-rewrite-objc'. The
ordering is necessary due to the way Clang parses its options.

Note that this is no longer true, the option parser tblgen backend
handles the reordering now.

  - Daniel

Ok, then I guess I used an older version of Clang.

Hi Jacob!

Jacob Carlborg meinte am 12.09.2010 22:30:

<FlagClass Name:"-rewrite-objc">
<FlagClass Name:"-rewrite-objc-to-d">
Assertion failed: (0&& "Options are not in order!"), function OptTable,
file OptTable.cpp, line 119.

[...]

So how do I correctly add a new command line option to Clang

You just have to order them correctly [1]:

> Options are stored in sorted order, with '\0' at the end of the
> alphabet.o

So '-rewrite-objc-to-d' has to come *before* '-rewrite-objc'. The
ordering is necessary due to the way Clang parses its options.

Thanks that helped, but how could "-rewrite-objc-to-d" come before "-rewrite-objc" ?

Hi Jacob!

Jacob Carlborg meinte am 12.09.2010 22:30:

<FlagClass Name:"-rewrite-objc">
<FlagClass Name:"-rewrite-objc-to-d">
Assertion failed: (0&& "Options are not in order!"), function OptTable,
file OptTable.cpp, line 119.

[...]

So how do I correctly add a new command line option to Clang

You just have to order them correctly [1]:

> Options are stored in sorted order, with '\0' at the end of the
> alphabet.o

So '-rewrite-objc-to-d' has to come *before* '-rewrite-objc'. The
ordering is necessary due to the way Clang parses its options.

Thanks that helped, but how could "-rewrite-objc-to-d" come before
"-rewrite-objc" ?

That is just the particular definition of the ordering we use. We
always match the first option in the list which is a prefix (more or
less), and we want "-rewrite-objc-to-d" to be matched instead of
"-rewrite-objc" (if it accepts suffixes).

- Daniel

Thanks I understand now, forgot about the null character.