Class: AdLint::Cpp::PPTokens

Inherits:
SyntaxNode show all
Defined in:
lib/adlint/cpp/syntax.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from SyntaxNode

#short_class_name

Methods included from LocationHolder

#analysis_target?

Methods included from Visitable

#accept

Constructor Details

#initializePPTokens

Returns a new instance of PPTokens.



572
573
574
575
# File 'lib/adlint/cpp/syntax.rb', line 572

def initialize
  super
  @tokens = []
end

Instance Attribute Details

#tokensObject (readonly)

Returns the value of attribute tokens.



577
578
579
# File 'lib/adlint/cpp/syntax.rb', line 577

def tokens
  @tokens
end

Instance Method Details

#inspect(indent = 0) ⇒ Object



704
705
706
# File 'lib/adlint/cpp/syntax.rb', line 704

def inspect(indent = 0)
  " " * indent + self.to_s
end

#locationObject



584
585
586
# File 'lib/adlint/cpp/syntax.rb', line 584

def location
  @tokens.first.location
end

#may_represent_block?Boolean

Returns:

  • (Boolean)


628
629
630
631
632
633
634
635
636
# File 'lib/adlint/cpp/syntax.rb', line 628

def may_represent_block?
  return false if @tokens.size < 2

  if @tokens.first.value == "{" && @tokens.last.value == "}"
    @tokens.any? { |pp_tok| pp_tok.value == ";" }
  else
    false
  end
end

#may_represent_controlling_keyword?Boolean

Returns:

  • (Boolean)


688
689
690
691
692
693
694
695
696
697
698
# File 'lib/adlint/cpp/syntax.rb', line 688

def may_represent_controlling_keyword?
  return false if @tokens.size > 1

  case @tokens.first.value
  when "while", "do", "for", "if", "else", "switch", "case", "default",
    "goto", "return", "break", "continue"
    true
  else
    false
  end
end

#may_represent_declaration_specifiers_head?Boolean

Returns:

  • (Boolean)


662
663
664
665
666
667
668
669
670
671
672
673
# File 'lib/adlint/cpp/syntax.rb', line 662

def may_represent_declaration_specifiers_head?
  @tokens.all? do |pp_tok|
    case pp_tok.value
    when "typedef", "extern", "static", "auto", "register"
      true
    when "const", "volatile", "restrict"
      true
    else
      false
    end
  end
end

#may_represent_do_while_zero_idiom?Boolean

Returns:

  • (Boolean)


638
639
640
641
642
643
644
# File 'lib/adlint/cpp/syntax.rb', line 638

def may_represent_do_while_zero_idiom?
  return false if @tokens.size < 4

  @tokens[0].value == "do" && @tokens[-4].value == "while" &&
    @tokens[-3].value == "(" && @tokens[-2].value == "0" &&
    @tokens[-1].value == ")"
end

#may_represent_expression?Boolean

Returns:

  • (Boolean)


588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
# File 'lib/adlint/cpp/syntax.rb', line 588

def may_represent_expression?
  return false if @tokens.size < 2

  @tokens.all? do |pp_tok|
    case pp_tok.value
    when "{", "}"
      false
    when ";"
      false
    when "while", "do", "for", "if", "else", "switch", "case", "default",
         "goto", "return", "break", "continue"
      false
    when "typedef", "extern", "static", "auto", "regisiter"
      false
    else
      true
    end
  end
end

#may_represent_initializer?Boolean

Returns:

  • (Boolean)


608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
# File 'lib/adlint/cpp/syntax.rb', line 608

def may_represent_initializer?
  return false if @tokens.size < 2

  if @tokens.first.value == "{" && @tokens.last.value == "}"
    @tokens.all? do |pp_tok|
      case pp_tok.value
      when "while", "do", "for", "if", "else", "switch", "case", "default",
           "goto", "return", "break", "continue"
        false
      when ";"
        false
      else
        true
      end
    end
  else
    false
  end
end

#may_represent_punctuator?Boolean

Returns:

  • (Boolean)


684
685
686
# File 'lib/adlint/cpp/syntax.rb', line 684

def may_represent_punctuator?
  @tokens.size == 1 && PUNCTUATORS.include?(@tokens.first.value)
end

#may_represent_specifier_qualifier_list?Boolean

Returns:

  • (Boolean)


646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
# File 'lib/adlint/cpp/syntax.rb', line 646

def may_represent_specifier_qualifier_list?
  @tokens.select { |pp_tok|
    case pp_tok.value
    when "const", "volatile", "restrict"
      true
    when "*"
      true
    when "void", "signed", "unsigned", "char", "short", "int", "long",
         "float", "double"
      true
    else
      false
    end
  }.size > 1
end

#push(tok) ⇒ Object



579
580
581
582
# File 'lib/adlint/cpp/syntax.rb', line 579

def push(tok)
  @tokens.push(tok)
  self
end

#to_sObject



700
701
702
# File 'lib/adlint/cpp/syntax.rb', line 700

def to_s
  @tokens.map { |tok| tok.value }.join(" ")
end