Does specifying parameters or variables as __attribute__ ((unused)) allow the compiler to perform any additional optimizations?

StackOverflow https://stackoverflow.com/questions/14388067

  •  16-01-2022
  •  | 
  •  

문제

I'm particularly curious about LLVM 4.1, but would be interested in other compilers' behavior as well.

According to the GCC documentation (which LLVM supports at least in part), the unused attribute has the following behavior:

This attribute, attached to a variable, means that the variable is meant to be possibly unused. GCC will not produce a warning for this variable. 

If the compiler is able to warn you about unused parameters and variables, though, presumably it already knows what parameters and variables are unused without you having to tell it (especially since the unused attribute only indicates that the variable is possibly unused). Therefore, does the unused attribute allow the compiler to perform any additional optimizations, or is its purpose just to allow for more readable code? Also, if the unused attribute does in fact allow the compiler to perform additional optimizations, what happens if you actually end up using a parameter or variable that was specified as unused? LLVM (in XCode) did not seem to complain about this case, though it's possible I wasn't compiling at the right optimization level or with the right warnings enabled.

도움이 되었습니까?

해결책

__attribute__((unused)) doesn't help optimization, and it doesn't mean that the value is necessarily unused. It suppresses warning (if there is a reason for this warning, that is, if the value is indeed unused), that's all.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top