|
| 1 | +import go |
| 2 | + |
| 3 | +/** |
| 4 | + * Holds if there exists a comment on the same line as `l` |
| 5 | + * that contains the substring "`kind`,`dep`,`ver`". |
| 6 | + */ |
| 7 | +predicate metadata(Locatable l, string kind, string mod, string dep, string ver) { |
| 8 | + exists(string f, int line, Comment c, string text | |
| 9 | + l.hasLocationInfo(f, line, _, _, _) and |
| 10 | + c.hasLocationInfo(f, line, _, _, _) |
| 11 | + | |
| 12 | + text = c.getText().regexpFind("\\b([^,\\s]+,[^,]+,[^,]+,[^,\\s]+)", _, _) and |
| 13 | + kind = text.regexpCapture("([^,]+),([^,]+),([^,]+),([^,]+)", 1) and |
| 14 | + mod = text.regexpCapture("([^,]+),([^,]+),([^,]+),([^,]+)", 2) and |
| 15 | + dep = text.regexpCapture("([^,]+),([^,]+),([^,]+),([^,]+)", 3) and |
| 16 | + ver = text.regexpCapture("([^,]+),([^,]+),([^,]+),([^,]+)", 4) |
| 17 | + ) |
| 18 | +} |
| 19 | + |
| 20 | +query predicate missingRequire(string mod, string dep, string ver, int line) { |
| 21 | + exists(Locatable l | metadata(l, "RequireLine", mod, dep, ver) | |
| 22 | + l.hasLocationInfo(_, line, _, _, _) |
| 23 | + ) and |
| 24 | + not exists(GoModRequireLine req | |
| 25 | + req.getModulePath() = mod and |
| 26 | + req.getPath() = dep and |
| 27 | + req.getVersion() = ver and |
| 28 | + metadata(req, "RequireLine", mod, dep, ver) and |
| 29 | + req.hasLocationInfo(_, line, _, _, _) |
| 30 | + ) |
| 31 | +} |
| 32 | + |
| 33 | +query predicate missingExclude(string mod, string dep, string ver, int line) { |
| 34 | + exists(Locatable l | metadata(l, "ExcludeLine", mod, dep, ver) | |
| 35 | + l.hasLocationInfo(_, line, _, _, _) |
| 36 | + ) and |
| 37 | + not exists(GoModExcludeLine exc | |
| 38 | + exc.getModulePath() = mod and |
| 39 | + exc.getPath() = dep and |
| 40 | + exc.getVersion() = ver and |
| 41 | + metadata(exc, "ExcludeLine", mod, dep, ver) and |
| 42 | + exc.hasLocationInfo(_, line, _, _, _) |
| 43 | + ) |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Holds if there exists a comment on the same line as `l` |
| 48 | + * that contains the substring "ReplaceLine,`mod`,`dep`,`dver`,`rep`,`rver`". |
| 49 | + */ |
| 50 | +predicate repmetadata(Locatable l, string mod, string dep, string dver, string rep, string rver) { |
| 51 | + exists(string f, int line, Comment c, string text | |
| 52 | + l.hasLocationInfo(f, line, _, _, _) and |
| 53 | + c.hasLocationInfo(f, line, _, _, _) |
| 54 | + | |
| 55 | + text = c.getText().regexpFind("\\b(ReplaceLine,[^,]*,[^,]*,[^,]*,[^,]*,[^,\\s]*)", _, _) and |
| 56 | + mod = text.regexpCapture("ReplaceLine,([^,]*),([^,]*),([^,]*),([^,]*),([^,]*)", 1) and |
| 57 | + dep = text.regexpCapture("ReplaceLine,([^,]*),([^,]*),([^,]*),([^,]*),([^,]*)", 2) and |
| 58 | + dver = text.regexpCapture("ReplaceLine,([^,]*),([^,]*),([^,]*),([^,]*),([^,]*)", 3) and |
| 59 | + rep = text.regexpCapture("ReplaceLine,([^,]*),([^,]*),([^,]*),([^,]*),([^,]*)", 4) and |
| 60 | + rver = text.regexpCapture("ReplaceLine,([^,]*),([^,]*),([^,]*),([^,]*),([^,]*)", 5) |
| 61 | + ) |
| 62 | +} |
| 63 | + |
| 64 | +query predicate missingReplace(string mod, string dep, string dver, string rep, string rver, int line) { |
| 65 | + exists(Locatable l | repmetadata(l, mod, dep, dver, rep, rver) | |
| 66 | + l.hasLocationInfo(_, line, _, _, _) |
| 67 | + ) and |
| 68 | + not exists(GoModReplaceLine repl | |
| 69 | + ( |
| 70 | + rver = repl.getReplacementVersion() |
| 71 | + or |
| 72 | + not exists(repl.getReplacementVersion()) and |
| 73 | + rver = "" |
| 74 | + ) and |
| 75 | + ( |
| 76 | + dver = repl.getOriginalVersion() |
| 77 | + or |
| 78 | + not exists(repl.getOriginalVersion()) and |
| 79 | + dver = "" |
| 80 | + ) |
| 81 | + | |
| 82 | + repl.getModulePath() = mod and |
| 83 | + repl.getOriginalPath() = dep and |
| 84 | + repl.getReplacementPath() = rep and |
| 85 | + repmetadata(repl, mod, dep, dver, rep, rver) and |
| 86 | + repl.hasLocationInfo(_, line, _, _, _) |
| 87 | + ) |
| 88 | +} |
0 commit comments