Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-5297

GNU attributes should be used correctly

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way, MISRA C++ 2008 recommended
    • Covered Languages:
      C, C++, Objective-C
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      10min
    • Analysis Level:
      Semantic Analysis

      Description

      _attribute_ is a GNU extension that allows to decorate functions, parameters, variables... with some attributes. It may help for compiler optimizations or for the writer of some code to better state his intent (and have the compiler check it).

      If this extension is used incorrectly, it will usually not break the build, but it still means that the code may not behave as the developer expects. This rule reports such occurrences of bad use of _attribute_.

      Noncompliant Code Example

      int f1() __attribute__((returns_nonnull)); // Noncompliant; "returns_nonnull" only applies to return values which are pointers
      
      void g(int *a) __attribute__((nonnull(1))){} // Noncompliant; "nonnull" position in the function definition is not allowed
      
      void h() __attribute__((warn_unused_result)); // Noncompliant; "warn_unused_result" does not work with function without return value
      
      void test() {
        int __declspec(empty_bases)i; // Noncompliant; "empty_bases" only applies to classes  
        char c = (char __attribute__((aligned(8)))) i; // Noncompliant, attribute is ignored
      }}
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              amelie.renard Amélie Renard
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: