> On Apr 24, 2020, at 2:44 PM, Richard Smith via cfe-users 
> <cfe-users@lists.llvm.org> wrote:
> 
> On Tue, 21 Apr 2020 at 05:41, Dr S3curity via cfe-users 
> <cfe-users@lists.llvm.org <mailto:cfe-users@lists.llvm.org>> wrote:
> Hi,
> 
> Imagine the very basic code below, it has some missing elements, we dont have 
> the foo() function and MyClass class,
> 
> ```test.cpp
> int main(int argc, char *argv[])
> {
>     MyClass* mc = new MyClass();
>     mc.get_count();
>     foo(1, 2, 3);
>     std::cout << "this is a test" << endl;
> }
> ```
> 
> How can I get AST for that code?
> I tried `clang -Xclang -ast-dump -fsyntax-only test.cpp`, I can get the AST 
> tree but without MyClass and foo() function, it doesn't even say foo is a 
> function with 3 integer parameters.

I am not really familiar with -frecovery-ast but since even such relatively 
simple C++ snippet is ambivalent I am not sure clang can actually answer your 
questions completely :(
It would have to "know" things like the following before it could tell you what 
"foo" or "MyClass" are.

- Can't foo be a global object of class with call operator accepting 3 int-s?
- Do the parameters actually need to be int-s or just any types that have 
implicit conversions from int?
- Can't MyClass be a typedef?
- Can't macros be involved?
- Can't the code actually be semantically incorrect?

> This can be problematic for different cases for example IDE's where you dont 
> have all the project dependencies but yet you need to know what are the 
> elements in your code.
> 
> Is that possible to do with libclang? Any other suggestions are welcome.
> 
> You can request that more invalid portions of the AST are retained by using 
> `-Xclang -frecovery-ast`. In this case, that will preserve the function call 
> expression as a RecoveryExpr. For now at least, we don't have the ability to 
> preserve AST information for the invalid "new MyClass" nor the expression 
> using of undeclared "std::cout" and "endl", but -frecovery-ast is expected to 
> improve over time to retain more such AST nodes. (And perhaps eventually it 
> will be enabled by default, at which point the flag for it might disappear. 
> We don't guarantee any forward compatibility with -Xclang flags.)

IIUC you'd still have to decide what you think the RecoveryExpr AST node 
actually is - it could be a CXXMemberCallExpr if it's a call operator or just a 
CallExpr if foo is a free function.

> _______________________________________________
> cfe-users mailing list
> cfe-users@lists.llvm.org <mailto:cfe-users@lists.llvm.org>
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users 
> <https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users>
_______________________________________________
cfe-users mailing list
cfe-users@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users

Reply via email to