Question about Decl::isUsed()

Hi everyone,

I’m trying to write a tool to remove unused declarations from preprocessed files but found an odd behaviour when using the Decl::isUsed() call.

It works great with clang::FunctionDecl and clang::ValueDecl, but when I try to check the flag for clang::TagDecl or clang::TypedefNameDecl, it’s always false, even if it’s used in the preprocessed file.

Is that the expected behaviour? I know that there might be problems when dealing with cross TUs but I have a single preprocessed file and everything is marked as unused.

Thank you,

You might want to read through https://reviews.llvm.org/D46190 which has some remarks from Richard Smith (Clang code owner) about Used versus Referenced.

Also might point out how these tracking flags might not be perfectly handled in things that don’t turn into object-file entities, such as typedefs.

–paulr

Thanks for the tip, Paul! I tried the patch and it definitely helped removing a number of unreferenced typedefs. I didn’t help with the TagDecls though.

Also might point out how these tracking flags might not be perfectly handled in things that don’t turn into object-file entities, such as typedefs.

I see, maybe I should create a visitor to replace all typedefs with the underlying type, then remove all typedefs.

I guess it should work since I’m only using preprocessed files.

Thank you,