Any C / C ++ refactoring tool based on libclang? (even the simplest “toy example”)

As I have already pointed out - here - it seems that clang libclang should be excellent for the implementation of a difficult task, which is the analysis and modification of C / C ++ code ( watch the video presentation and slides ).

Do you know about any C / C ++ libclang based refactoring tool?

"Any" includes even a simple alpha state project with support for one refactorization method. It may be without preprocessor support. As an example, what I'm talking about functionally: changing the names of methods, regardless of whether it supports multiple files or only one file at a time. You might be wondering what the goal is to ask for even small working examples. My thought is that creating a list of code examples and small tools that are in one place will provide the best resource to learn how to implement refactoring with libclang. I believe that larger projects can be developed from simple projects - in the right form with open source :).

+45
c ++ clang refactoring llvm
Nov 01. '11 at 15:54
source share
8 answers

Clang contains a library called "CIndex", which I suppose was designed to run code in the IDE. It can also be used for parsing C ++ and navigating through AST, but has no way to refactor it. See Eli Bendersky's article here .

I recently launched such a project: cmonster . This is a Python-based API for C ++ parsing (using libclang), AST parsing, with an interface for "rewriting" (i.e., Insert / delete / change source ranges). There is no good way (yet) for doing things like changing the names of functions and converting them to the original modifications, but it would not be so difficult to do.

I have not created an issue with this functionality yet (although this is also in the github repo), since I am waiting for llvm / clang 3.0 to be released.

In addition, I have to point out a few things:

  • The code is very rude, calling it alpha might be generous.
  • I am by no means an expert on this issue (in contrast, say, Dr. Ira Baxter).

Adjust your expectations accordingly.

Update: cmonster 0.2 has been released, which includes the features described. Check out on github .

+22
Nov 02 2018-11-11T00:
source share

Google is working on a tool library for Clang. Starting with version 3.2 . It includes the ASTMatchers library, so you can just create a query and do not have to pass the AST.

There is an excellent video chat on this subject that looks at a simple renaming example. (This is from the same guy as the MapReduce Discussion published above, but a newer and simpler practical implementation, rather than Google’s internal design and scale of the enterprise, is ongoing).

The source for this example, which renames the method, is available in the branch. It might be somewhere in the trunk, but I can't find it. Renaming the getDeclAs function to getNodesAs, as well as another, is clearly not recommended .). There is a more extended example that removes duplicate calls to c_str (which is in the trunk and someone above).

Here is the documentation for LibASTMatchers and LibTooling .

EDIT: Some of the best docs for ASTMatcher. Here and here .

EDIT: Google is now working on what is called Clangd , whose purpose is some kind of Clang server for refactoring.

+13
Dec 12 '12 at 13:27
source share

Google has created a Clang-based refactoring tool for its C ++ code base and plans to publish it. I don’t know the current status of the project, but you can see this demo presented at the LLVM 2011 developer meeting: https://www.youtube.com/watch?v=mVbDzTM21BQ .

In addition, the built-in Xcode (4+) auto-completion and refactoring functions are based on libclang.

+8
Jan 16 2018-12-12T00:
source share

It may be a bit of a “meta", but there is an example written in clang as a tool to run on clang (although there is more than just that.

RemoveCStrCalls.cpp

// This file implements a tool that prints replacements that remove redundant // calls of c_str() on strings. // // Usage: // remove-cstr-calls <cmake-output-dir> <file1> <file2> ... // // Where <cmake-output-dir> is a CMake build directory in which a file named // compile_commands.json exists (enable -DCMAKE_EXPORT_COMPILE_COMMANDS in // CMake to get this output). // // <file1> ... specify the paths of files in the CMake source tree. This path // is looked up in the compile command database. If the path of a file is // absolute, it needs to point into CMake source tree. If the path is // relative, the current working directory needs to be in the CMake source // tree and the file must be in a subdirectory of the current working // directory. "./" prefixes in the relative files will be automatically // removed, but the rest of a relative path must be a suffix of a path in // the compile command line database. // // For example, to use remove-cstr-calls on all files in a subtree of the // source tree, use: // // /path/in/subtree $ find . -name '*.cpp'| // xargs remove-cstr-calls /path/to/source 
+6
Dec 09 2018-11-12T00:
source share

Not open source, but was used to perform a very non-toy massive automated refactoring of C ++ programs : our DMS Software Reengineering Toolkit . DMS is a "library" (we call it "toolkit") of objects that can be compiled to achieve anlaysis and / or automatic translation.

Regarding C ++, DMS provides at this point in time:

  • A complete C ++ 11 analyzer creating AST and able to accurately restore source code including comments, with a full preprocessor
  • Full C ++ parser with name and type resolution for C ++ (ANSI, GNU, MS Visual C ++)
  • Control flow analysis for C ++
  • Convert source to source
  • The “renaming” mechanism has been partially completed (see below).

From experience, I can say that C ++ is the bitch of a language for conversion.

We continue to work on this and complete a reliable renaming tool. Even this is difficult; the key issue is the name-shading problem. You have a local variable X and a reference to Y inside this area; you are trying to rename Y to X and find that the local variable is "capturing" access. It's amazing how many namespaces and capture types you need to worry about in C ++. And this is necessary as the basis for many other refactoring.

EDIT Feb 2014: full C ++ 14 analyzer, control flow analysis, local data analysis

+5
Nov 05 '11 at 3:15
source share

https://github.com/lukhnos/refactorial based on clang and claims

Conversions Provided

Accessory: synthesize getters and setters for the assigned element Variables

MethodMove: move inline elements of element functions to file implementation

ExtractParameter: push function variable to parameter Function

TypeRename: rename types, including tag types (enum, struct, union, class), template classes, Objective-C types (class and protocol), typedefs and even bulit-in types (e.g. unsigned to uint32_t)

RecordFieldRename: rename records (struct, union), including C ++ member variables

Function Rename: rename functions, including C ++ member functions

Works through the specifications in the YAML configuration file. I have not tried (yet).

+5
Apr 04
source share

Another option is to create your own GCC plugin or develop the GCC MELT extension to complete your task. But the GCC (or Clang) extension requires an understanding of the internal representations of these compilers (Gimple and Tree for GCC), and this requires some work. MELT is a high-level domain language for the GCC extension.

+3
Nov 02 2018-11-11T00:
source share

This is not refactoring, but completion, but may be useful:

+2
Jan 02 2018-12-12T00:
source share



All Articles