REFERENCES_FILE from the Go file to every file its //go:embed lines name; * `files` is every File node sorted by path, so a directory's files are one * range. */
| 905 | * `files` is every File node sorted by path, so a directory's files are one |
| 906 | * range. */ |
| 907 | static int op_embed(cbm_gbuf_t *gb, const cbm_gbuf_node_t *go_file, const char *text, |
| 908 | const cbm_gbuf_node_t **files, int nfiles) { |
| 909 | char dir[CBM_SZ_1K]; |
| 910 | op_dirname(go_file->file_path, dir, sizeof(dir)); |
| 911 | size_t dl = strlen(dir); |
| 912 | int linked = 0; |
| 913 | for (const char *p = strstr(text, "//go:embed "); p; p = strstr(p + SKIP_ONE, "//go:embed ")) { |
| 914 | if (p != text && p[-SKIP_ONE] != '\n') { |
| 915 | continue; /* a directive starts its line */ |
| 916 | } |
| 917 | const char *eol = strchr(p, '\n'); |
| 918 | char line[CBM_SZ_1K]; |
| 919 | const char *args = p + OP_SLEN("//go:embed "); |
| 920 | snprintf(line, sizeof(line), "%.*s", eol ? (int)(eol - args) : (int)strlen(args), args); |
| 921 | for (char *tok = line; *tok;) { |
| 922 | while (*tok == ' ' || *tok == '\t' || *tok == '\r') { |
| 923 | tok++; |
| 924 | } |
| 925 | char *e = tok; |
| 926 | while (*e && *e != ' ' && *e != '\t' && *e != '\r') { |
| 927 | e++; |
| 928 | } |
| 929 | char save = *e; |
| 930 | *e = '\0'; |
| 931 | bool all = strncmp(tok, "all:", OP_SLEN("all:")) == 0; |
| 932 | const char *pattern = all ? tok + OP_SLEN("all:") : tok; |
| 933 | if (pattern[0] && pattern[0] != '"' && pattern[0] != '`') { |
| 934 | /* files below dir: the sorted range starting at "dir/" */ |
| 935 | int lo = 0; |
| 936 | int hi = dl ? nfiles : 0; |
| 937 | while (lo < hi) { |
| 938 | int mid = lo + (hi - lo) / PAIR_LEN; |
| 939 | if (strncmp(files[mid]->file_path, dir, dl) < 0 || |
| 940 | (strncmp(files[mid]->file_path, dir, dl) == 0 && |
| 941 | files[mid]->file_path[dl] < '/')) { |
| 942 | lo = mid + SKIP_ONE; |
| 943 | } else { |
| 944 | hi = mid; |
| 945 | } |
| 946 | } |
| 947 | for (int i = lo; i < nfiles; i++) { |
| 948 | const char *fp = files[i]->file_path; |
| 949 | if (dl && (strncmp(fp, dir, dl) != 0 || fp[dl] != '/')) { |
| 950 | break; |
| 951 | } |
| 952 | const char *rel = dl ? fp + dl + SKIP_ONE : fp; |
| 953 | if (files[i] != go_file && op_embeds(pattern, rel, all)) { |
| 954 | cbm_gbuf_insert_edge(gb, go_file->id, files[i]->id, "REFERENCES_FILE", |
| 955 | "{\"via\":\"go:embed\"}"); |
| 956 | linked++; |
| 957 | } |
| 958 | } |
| 959 | } |
| 960 | *e = save; |
| 961 | tok = e; |
| 962 | } |
| 963 | } |
| 964 | return linked; |
no test coverage detected