简体   繁体   中英

Parsing C++ files for edit

I have over 200 Cpp-Provider-Files for my information-model in which I want to add some lines of code.

namespace project {
//Here i want to add a member
// other members

ANDBase::ANDBase(
{
//Constructor Impl
//In the last line of the Constructor i want to add some code
}
}

the provider-Files are all build the same way and have an identical semantic, so I look for a parser which would have to manage only same behavioral cpp files. Is there software (pref. Visual studio project include) which can help me?

Sounds like a job for Clang's LibTooling .

There you can parse the code and then search the AST (Abstract Syntax Tree) for the constructs you care about, make changes and write the modifications back to the source file(s).

This is kind of like doing aspect oriented programming with an **after* specification. AspectC++ exists as a research project, but I don't think it is active or will work with modern C++(14).

Our DMS Software Reengineering Toolkit with its C++14 Front End can probably do this.

(Some people imagine that parsing C++ is easy. It is not. )

DMS parses C++ to ASTs , allows you to apply source-to-source transformations to modify those ASTs, and then can regenerate valid C++ text from the modified trees, including preserving comments.

The source-to-source transformations are written in terms of C++ source-level syntax, and look like this:

       if you see *this*, replace it by *that* when *somecondition*

where this and that are source patterns with placeholder variables, and somecondition checks that the transformation should be applied.

DMS can/does build symbol tables for the source code. Often the somecondition checks that a symbol of interest has the right properties to trigger a rule.

In OP's case, he wants a rule something like:

 rule insert_into_constructor(q:qualified_path, ss: statement_sequence):
       statement_sequence -> statement_sequence
 =  " \q () { \ss } "  -- left hand side pattern *this*
 -> " \q () { \ss \mycode\(\) } " -- right hand side pattern *that*
 if is_in_namespace("foo", q); -- *when* condition

\\mycode can be inlined C++ syntax, or written as follows:

 pattern my_code(): statement_sequence =
    "  <arbitrayC++code> " ;

The metaquotes " are used to seperate the syntax of the rule language from the syntax of ++ code. The \\n you see inside the metaquotes are placeholder variables, that represent the syntactic structure declared for n . A placeholder in the left pattern of a rule is matched against code in the program; the same placeholder on the right hand side pattern (after -> ) is replaced by what got matched in the left hand side. The is_in_namespace metafunction checks that a found constructor is in the namespace that you want to modify. You might want a different check here; this is intended only to be suggestive. Note that the matched metavariable q is passed to the when condition; it doesn't need a "\\" because it isn't inside metaquotes.

So, you can use DMS to implement aspect-oriented programming. It isn't limited to that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM