Status of the C++11 Migrator----Monday, April 15, 2013

Since the design document for cpp11-migrate, the C++11 migrator tool, was first proposed in early
December 2012 development has been making steady progress. In this article I'll talk about what's been implemented in cpp11-migrate so far, what's coming up, and how you can get involved.

The purpose of the C++11 Migrator is to do source-to-source translation to migrate existing C++ code to use C++11 features to enhance maintainability, readability, runtime performance, and compile-time performance. Development is still early and transforms
fall mostly into the first two categories. The migrator is based on Clang's LibTooling and the AST
Matching library
.

Most of the development so far has been carried out by a small core group at Intel. Our focus so far has been to set up project infrastructure and testing, implement a few basic transforms, and make sure those transforms work well. Our aim is to make this tool
useful to the community so we're always listening for transform ideas and feedback.

How to Get cpp11-migrate

cpp11-migrate is located in the Extra Clang Tools repository. To build cpp11-migrate, you will need the LLVM and Clang sources as well. Follow the directions in Clang's Getting
Started instructions
 making sure to perform the optional step of checking out the Extra Clang Tools repository. Once checked out into the correct directory the build system, after a reconfiguration, will automatically include the extra Clang tools as part
of the next full build. If you're using the CMake build system, you can build just cpp11-migrate with the cpp11-migratetarget. The check-clang-tools target
provided by CMake will run all regression tests for extra Clang tools, including cpp11-migrate.

The Transforms So Far

The C++11 Migrator currently supports four features of C++11:

  • Range-based for loops
  • The nullptr literal for null pointers
  • The auto type specifier
  • override virtual specifier

The range-based for-loop transform once existed as a stand-alone tool called loop-convert contributed by Sam Panzer. When
development on more transforms started, the idea became to pull all transforms under the jurisdiction of a single tool and cpp11-migrate was born. The
range-based for-loop transform replaces for-loops used in one of the following three common situations: 

  1. Loops over containers using iterators

    std::vector<int> myVec;
    for (std::vector<int>::iterator I = myVec.begin(),
                                    E = myVec.end();
         I != E; ++I)
      llvm::outs() << *I;
    

     


     

    std::vector<int> myVec;
    for (auto & elem : myVec)
      llvm::outs() << elem;
    
  2. Loops over statically allocated arrays
    int arr[] = {1,2,3,4,5};
    for (int i = 0; i < 5; ++i)
      llvm::outs() << arr[i];
    

     


     

    int arr[] = {1,2,3,4,5};
    for (auto & elem : arr)
      llvm::outs() << elem;
    
  3. Loops over array-like containers using operator[] or at().
    std::vector<int> myVec;
    for (int i = 0; i < myVec.size(); ++i)
      llvm::outs() << v[i];
    

     


     

    std::vector<int> myVec;
    for (auto & elem : myVec)
      llvm::outs() << elem;
    

The nullptr transform uses the new nullptr literal where pointers are being initialized with or assigned a null value. In cases where an explicit cast is used,
the explicit cast is left behind to avoid introducing ambiguities into the code.

void foo(int *arg);
void foo(float *arg);

int *IntPtr = 0;
float *FloatPtr = NULL;
foo(static_cast<int*>(0));

 


 

void foo(int *arg);
void foo(float *arg);

int *IntPtr = nullptr;
float *FloatPtr = nullptr;
foo(static_cast<int*>(nullptr));

The auto type specifier transform replaces the type specifier for variable declarations with the new auto keyword. In general, such a replacement can be done
whenever the type of the variable declaration matches the type of its initializer. However, the transform targets only a few specific useful situations with readability and maintainability in mind:

  1. When the variable is an iterator for an STL container.

    std::vector<std::pair<int, std::string> >::iterator NameAgeI = People.begin();
    for (std::vector<MyType>::iterator I = Container.begin(),
                                       E = Container.end;
         I != E; ++I) {
      // ...
    }
    

     


     

    auto NameAgeI = People.begin();
    for (auto I = Container.begin(),
                                      E = Container.end;
         I != E; ++I) {
      // ...
    }
    
  2. When the initializer is an allocation using the new operator.
    MyType *VarPtr = new MyType();
    MyType * const VarCPtr = new MyType();
    

     


     

    auto VarPtr = new MyType();
    auto const VarCPtr = new MyType();
    

Support for a third situation is in development: creating objects with factory functions.

MyType *FooPtr = makeObject<MyType>(/*...*/);
MyType *BarPtr = MyType::create(/*...*/);

 


 

auto FooPtr = makeObject<MyType>(/*...*/);
auto BarPtr = MyType::create(/*...*/);

In each situation, the deduced type for the declared variable should be obvious to the reader. Iterators for standard containers are created by functions with specific names and are used in specific situations. For factory functions and operator new, the
type is spelled out in the initializer so repeating it in the variable declaration is not necessary.

The override virtual specifier transform, contributed by Philip Dunstan, is the migrator's fourth transform and the first to be contributed from outside the core group at Intel. This
transform detects virtual member functions in derived classes that override member functions from parent classes and adds the override virtual specifier to the function.

class Parent {
public:
  virtual int getNumChildren();
};

class Child {
public:
  virtual int getNumChildren();
};

 


 

class Parent {
public:
  virtual int getNumChildren();
};

class Child {
public:
  virtual int getNumChildren() override;
};

More details on these transforms, what they can and can't do, how to adjust their behaviour, and known limitations can be found in thecpp11-migrate
User's Manual
.

Testing on Real Projects

What better way to test the C++11 Migrator than to run it on entire real projects? We've set up a continuous integration server to build and run cpp11-migrate on two projects so far and have plans for at least three more. For each project, the goal is
to build the transformed code and run that project's test suite to ensure semantics haven't changed.

Implemented: 

  1. LLVM 3.1
  2. ITK 4.3.1

 

Planned: 

  1. LLDB
  2. OpenCV
  3. Poco

Running the migrator on real code has been enormously helpful for finding bugs. Real code from varying projects often reveals code expressions not accounted for in the development and unit testing of the transforms. Every time a bug found from transforming
these projects gets fixed, new test cases are added to the regression test suite and the migrator becomes more robust.

Future Work

Fixing bugs found by migrating real code is of high priority right now since we want a good user experience for as many people as we can as soon as possible. Adding more transforms is another priority and those transforms with the most interest from the
community will come first. Currently at the top of the list are:

  1. Use the standard library instead of TR1
  2. Replace use of the deprecated auto_ptr class.

In addition to fixing bugs and adding transforms, there are also more general improvements to consider. One such improvement we're making progress on is to remove the restriction that only source files are transformed and not any of the headers they include.
The restriction has been in place until now because the migrator needs to know which headers are safe to transform. System headers and third-party library headers clearly shouldn't be touched.

Get Involved!

If you want to get involved, the first thing you can do is try out cpp11-migrate on your code. Bugs can be logged with LLVM's bug tracker under the product clang-tools-extra.
Send an email to the Clang Developer's Mailing List if you need help or would like to get more involved. We look
forward to hearing from you!

Posted by Edwin Vane at 5:53
AM 

Labels: C++, Clang

Location: Waterloo, ON, Canada

时间: 2024-08-30 22:12:35

Status of the C++11 Migrator----Monday, April 15, 2013的相关文章

台积电11月营收15亿美元 同比增24%

网易科技讯 12月10日消息,据路透社报道,全球最大芯片代工厂商台积电(TSMC)于当地时间周一表示,该公司11月份销售额 同比增长23.9%,但环比下降11.4%.据这家台湾芯片制造厂商表示,上月未合并报表销售额达到436.4亿新台币(合15亿美元),而台积电去年同期销售额为352亿新台币,上月销售额则为493亿新台币.另外台积电上月合并报表销售额为443亿新台币,同比增长23.4%. (西风)

元芳你怎么看:全球11大CEO谁可能2013遇险?

2012年转眼将逝,处于风云变幻的科技媒体行业,我们与亿万 网友一同见证了无数难忘的时刻,不管是激动的.悲伤的还是快乐的,中国人常说,"温故而知新",斯时已逝,来日犹可追.我们为您拨云驱雾,抽取核心制成年终盘点大餐,以期知人,明事,窥理. 他们都曾是赫赫有名的操盘手,他们也曾带领千军万马在一片红海中杀出一条血路.然而,时至今日或许是理念与环境的冲突,亦或是廉颇老矣,他们头上的光环逐渐暗去.2013年他们就像走钢丝的人,步步惊心,稍有闪失遇便会跌入万丈深渊.下面的榜单中谁是你心中的&qu

新浪将于11月13日发布2013年第三季度财报

摘要: 查看最新行情 中国上海/2013年11月4日――服务于中国及全球华人社群的领先在线媒体公司 新浪 公司(Nasdaq GS: SINA)定于美国当地时间2013年11月12日周二股市收盘后公布截至2013年9月30日的  查看最新行情 中国上海/2013年11月4日――服务于中国及全球华人社群的领先在线媒体公司 新浪 公司(Nasdaq GS: SINA)定于美国当地时间2013年11月12日周二股市收盘后公布截至2013年9月30日的2013年第三季度未经审计的财务报告.随后,新浪管理

Google Summer of Code: C++ Modernizer Improvements----Monday, November 18, 2013

原文地址:http://blog.llvm.org/2013/11/google-summer-of-code-c-modernizer.html Google Summer of Code (GSoC) offers students stipends to participate in open source projects during the summer. This year, I was accepted to work on the Clang C++ Modernizer, a

【Oracle】Oracle 11gR2发布11.2.0.3 Patchset补丁集

11.2.0.3 Patch Set - Availability and Known Issues [ID 1348336.1]   修改时间 07-MAR-2012     类型 README     状态 PUBLISHED     Known Issues specific to the 11.2.0.3 Patch Set Please note that 11.2 Patch Sets 11.2.0.2 and higher are supplied as full releases

oracle数据库修改11.2 RAC 的 SCAN IP的例子

在某些情况下,由于是C/S架构,从以前的单机系统迁移到现在11.2的rac中,如果修改客户端ip地址工作量太大,而且也不现实,一般建议直接修改scan ip地址和以前一样,从而实现业务直接访问scan ip实现应用不用一个个单独配置.这里通过简单演示,实现修改scan ip的过程(网段不变),主要是把scan名字为scan-xff的ip地址从192.168.137.245修改为192.168.137.248 查看当前scan ip信息 [root-www.111cn.net@xff1 ~]# p

tomcat-web项目运行一段时间就宕掉了,警告: processCallbacks status 2

问题描述 web项目运行一段时间就宕掉了,警告: processCallbacks status 2 以下是日志信息,请大家帮忙看下: 2014-05-07 20:03:57 Commons Daemon procrun stderr initialized 2014-5-7 20:03:59 org.apache.catalina.core.AprLifecycleListener init 信息: The APR based Apache Tomcat Native library whic

11.2.0.3 Patch Set - Availability and Known Issues [ID 1348336.1]

11.2.0.3 Patch Set - Availability and Known Issues [ID 1348336.1] To Bottom Modified:16-Jan-2013Type:READMEStatus:PUBLISHEDPriority:3 Comments (0) Known Issues specific to the 11.2.0.3 Patch Set Please note that 11.2 Patch Sets 11.2.0.2 and higher ar

探索Oracle之数据库升级二 11.2.0.3升级到11.2.0.4完整步骤

探索Oracle之数据库升级二  11.2.0.3升级到11.2.0.4完整步骤   说明:         这篇文章主要是记录下单实例环境下Oracle 11.2.0.1升级到11.2.0.3的过程,当然RAC的升级是会有所不同.但是他们每个版本之间升级步骤都是差不多的,先升级Database Software,再升级Oracle Instance.  Oracle 11.2.0.4的Patchset No:19852360下载需要有Oracle Support才可以.  Patchset包含