| 1 | package pass |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mmcloughlin/avo/attr" |
| 5 | "github.com/mmcloughlin/avo/ir" |
| 6 | ) |
| 7 | |
| 8 | // IncludeTextFlagHeader includes textflag.h if necessary. |
| 9 | func IncludeTextFlagHeader(f *ir.File) error { |
| 10 | const textflagheader = "textflag.h" |
| 11 | |
| 12 | // Check if we already have it. |
| 13 | for _, path := range f.Includes { |
| 14 | if path == textflagheader { |
| 15 | return nil |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | // Add it if necessary. |
| 20 | if requirestextflags(f) { |
| 21 | f.Includes = append(f.Includes, textflagheader) |
| 22 | } |
| 23 | |
| 24 | return nil |
| 25 | } |
| 26 | |
| 27 | // requirestextflags returns whether the file uses flags in the textflags.h header. |
| 28 | func requirestextflags(f *ir.File) bool { |
| 29 | for _, s := range f.Sections { |
| 30 | var a attr.Attribute |
| 31 | switch s := s.(type) { |
| 32 | case *ir.Function: |
| 33 | a = s.Attributes |
| 34 | case *ir.Global: |
| 35 | a = s.Attributes |
| 36 | } |
| 37 | if a.ContainsTextFlags() { |
| 38 | return true |
| 39 | } |
| 40 | } |
| 41 | return false |
| 42 | } |