Method: AdLint::Cc1::VariableTable#storage_duration_of

Defined in:
lib/adlint/cc1/object.rb

#storage_duration_of(dcl_or_def) ⇒ Object



782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
# File 'lib/adlint/cc1/object.rb', line 782

def storage_duration_of(dcl_or_def)
  # NOTE: The ISO C99 standard says;
  #
  # 6.2.2 Linkages of identifiers
  #
  # 1 An identifier declared in different scopes or in the same scope more
  #   than once can be made to refer to the same object or function by a
  #   process called linkage.  There are three kinds of linkage: external,
  #   internal, and none.
  #
  # 3 If the declaration of a file scope identifier for an object or a
  #   function contains the storage-class specifier static, the identifier
  #   has internal linkage.
  #
  # 4 For an identifier declared with the storage-class specifier extern in
  #   a scope in which a prior declaration of that identifier is visible,
  #   if the prior declaration specifies internal or external linkage, the
  #   linkage of the identifier at the later declaration is the same as the
  #   linkage specified at the prior declaration. If no prior declaration
  #   is visible, or if the prior declaration specifies no linkage, then
  #   the identifier has external linkage.
  #
  # 5 If the declaration of an identifier for a function has no
  #   storage-class specifier, its linkage is determined exactly as if it
  #   were declared with the storage-class specifier extern. If the
  #   declaration of an identifier for an object has file scope and no
  #   storage-class specifier, its linkage is external.
  #
  # 6 The following identifiers have no linkage: an identifier declared to
  #   be anything other than an object or a function; an identifier
  #   declared to be a function parameter; a block scope identifier for an
  #   object declared without the storage-class specifier extern.
  #
  # 6.2.4 Storage durations of objects
  #
  # 1 An object has a storage duration that determines its lifetime. There
  #   are three storage durations: static, automatic, and allocated.
  #   Allocated storage is described in 7.20.3.
  #
  # 3 An object whose identifier is declared with external or internal
  #   linkage, or with the storage-class specifier static has static
  #   storage duration. Its lifetime is the entire execution of the program
  #   and its stored value is initialized only once, prior to program
  #   startup.
  #
  # 4 An object whose identifier is declared with no linkage and without
  #   the storage-class specifier static has automatic storage duration.

  if sc_spec = dcl_or_def.storage_class_specifier and
      sc_spec.type == :EXTERN || sc_spec.type == :STATIC
    :static
  else
    current_scope.global? ? :static : :automatic
  end
end