Book a Demo

Author Topic: CodeEngineering C macro handling  (Read 7731 times)

robertberlin

  • EA Novice
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
CodeEngineering C macro handling
« on: October 28, 2015, 08:35:13 pm »
Hello,

I want to import C source code into EA using EA11. Currently, the source code contains preprocessor macros used for function declaration like MYMACRO(void, MEMORYCLASS) myFunction() { ... }.

There is a stackoverflow thread (http://stackoverflow.com/questions/31131308/enterprise-architect-c-import-source-code-with-custom-macro-and-argument-in) dealing with this topic, but the example not complete.

Has someone experiences on this topic? What would be a good way to customize the way EA is importing the source code?

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: CodeEngineering C macro handling
« Reply #1 on: October 28, 2015, 09:54:36 pm »
You can tell EA about macros in the options.

OT: using macros will make a source schizophrenic. The best is to run the preprocessor with the options you want and feed the result to EA.

q.

Uffe

  • EA Practitioner
  • ***
  • Posts: 1859
  • Karma: +133/-14
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: CodeEngineering C macro handling
« Reply #2 on: October 28, 2015, 10:04:38 pm »
Hi Robert,


My experience is pretty limited, but the only way to fully customize source code import (as opposed to source code generation) in EA is to create your own grammar using the grammar framework.

There are some options you can set per language (Tools -- Options -- Source Code Engineering; Settings -- Preprocessor Macros), but they're limited. EA does not recognize cpp as a separate language, and it "handles" defined cpp macros by ignoring them.

Fundamentally, the issue is that C code with macros is not C code -- it's cpp code. If you feed the cpp code to the C compiler, it won't accept it either. The code has to go through the preprocessor to be turned into C.

The macros themselves can be used (are used) to essentially rewrite the C code at compile-time depending on the platform it's being compiled for, taking into account such things as endianism, address and word size, etc.

Thus, even if you create an EA grammar which can parse cpp code, you'll end up with a model of that cpp code, not a model of the C code. This is because the EA reverse-engineering process is in a sense single-pass: it creates a model from the source code. It does not parse the text, resolve macros, then creates a model from the resulting C code. That's just not how it works.

What you could do is create a cpp grammar and a cpp-to-C model transform. You would then end up with two models; one describing the cpp code and one describing the C code. That would be analogous to how the compilation process works.

However, cpp is a macro language and as such, UML is poorly suited to represent it. It can be done, but it won't be pretty. Recall that EA only attempts to create a structural model from the source code, not a behavioral one that would represent the sequences of statements inside the code blocks -- and what are the structural concepts of a macro language?

The alternative is to run the cpp code through the preprocessor, resulting in macro-free C code which you can then reverse-engineer into EA.

HTH,


/Uffe
My theories are always correct, just apply them to the right reality.

robertberlin

  • EA Novice
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Re: CodeEngineering C macro handling
« Reply #3 on: October 29, 2015, 11:47:25 pm »
Thank you for the replies. Maybe the link was a little bit confusing. I try to import ANSI-C code.

Quote
You can tell EA about macros in the options.

I only found an option to skip preprocessor macros. Could you please provide an example with two pramaters?

Quote
OT: using macros will make a source schizophrenic.

You're right. We bought that source code ;)

Quote
My experience is pretty limited, but the only way to fully customize source code import (as opposed to source code generation) in EA is to create your own grammar using the grammar framework.

Do you know if there is basic grammar available for 'C'? So I don't have to start from the scratch.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: CodeEngineering C macro handling
« Reply #4 on: October 30, 2015, 12:18:41 am »
There's only the possibility to define macros per name and as Uffe said EA will then evaluate them as (hmm) true and ignore the #else part (it might be vice versa as well; not that deep in those matters).

Personally I would not go the syntax file way. It's really a lot of work, error prone and except Uffe you will not find much support for it. It's really easier to expand the different macros as to what they shall be and import the created sources. There will then be different implementation paths in a PSM and you need to think or ways to put a scaffold around that for a PIM. That will be tricky enough.

q.
« Last Edit: October 30, 2015, 12:19:30 am by qwerty »