[cfe-dev] Is it safe to cast-away constness in RecursiveASTVisitors?

Sachkov, Alexey via cfe-dev cfe-dev at lists.llvm.org
Wed Jul 11 01:26:18 PDT 2018


Hi,

You could look into ConstStmtVisitor class: https://clang.llvm.org/doxygen/classclang_1_1ConstStmtVisitor.html

There is a little problem here that it is not a recursive visitor, but it is easy to solve. For example, look at https://clang.llvm.org/doxygen/CGException_8cpp_source.html, line 1645, CaptureFinder::Visit


From: cfe-dev [mailto:cfe-dev-bounces at lists.llvm.org] On Behalf Of Roman Popov via cfe-dev
Sent: Tuesday, July 10, 2018 9:41 PM
To: dtarditi at microsoft.com
Cc: Clang Dev <cfe-dev at lists.llvm.org>
Subject: Re: [cfe-dev] Is it safe to cast-away constness in RecursiveASTVisitors?

Thanks,
I wonder why const overloads are not provided in this case...

вт, 10 июл. 2018 г. в 11:23, David Tarditi <dtarditi at microsoft.com<mailto:dtarditi at microsoft.com>>:
It should be safe to do, as long as  the new code that you add to your visitor doesn’t modify AST objects.   I’ve done this in the past and haven’t run into any issues.

From: cfe-dev <cfe-dev-bounces at lists.llvm.org<mailto:cfe-dev-bounces at lists.llvm.org>> On Behalf Of Roman Popov via cfe-dev
Sent: Thursday, June 28, 2018 5:53 AM
To: Clang Dev <cfe-dev at lists.llvm.org<mailto:cfe-dev at lists.llvm.org>>
Subject: [cfe-dev] Is it safe to cast-away constness in RecursiveASTVisitors?

Hi all,

I want to implement RecursiveASTVisitor to find certain nodes in AST.  This class should not modify AST, so I want to make a search method to be const:

bool MyCallSearch::FindMyCall (const clang::Stmt *rootStmt ) const {
     this->TraverseStmt(stmt);
}

Unfortunately,  RecursiveASTVisitor methods are non-const and accept non-const pointers to AST nodes. So I wonder, is it safe to cast away constness like this ? :

bool MyCallSearch::FindMyCall (const clang::Stmt *rootStmt ) const {
     const_cast<MyCallSearch*>(this)->TraverseStmt(const_cast<clang::Stmt *>(stmt));
}

Thanks,
Roman

--------------------------------------------------------------------
Joint Stock Company Intel A/O
Registered legal address: Krylatsky Hills Business Park,
17 Krylatskaya Str., Bldg 4, Moscow 121614,
Russian Federation

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180711/6e1526fb/attachment-0001.html>


More information about the cfe-dev mailing list