grep: typesafe versions of grep_source_init

grep_source_init() can create "struct grep_source" objects and, depending on the value of the type passed, some void-pointer parameters have different meanings. Because one of these types (GREP_SOURCE_OID) will require an additional parameter in a subsequent patch, take the opportunity to increase clarity and type safety by replacing this function with individual functions for each type. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Reviewed-by: Matheus Tavares <matheus.bernardino@usp.br> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Aug 16, 2021 at 14:09 UTC 50d92b5f03f3c84d581b27cb8fa3a4b8cfbf2567
3 files changed +34 -23
builtin/grep.c
+2 -2
@@ -333,7 +333,7 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
333 struct grep_source gs;
334
335 grep_source_name(opt, filename, tree_name_len, &pathbuf);
336 - grep_source_init(&gs, GREP_SOURCE_OID, pathbuf.buf, path, oid);
336 + grep_source_init_oid(&gs, pathbuf.buf, path, oid);
337 strbuf_release(&pathbuf);
338
339 if (num_threads > 1) {
@@ -359,7 +359,7 @@ static int grep_file(struct grep_opt *opt, const char *filename)
359 struct grep_source gs;
360
361 grep_source_name(opt, filename, 0, &buf);
362 - grep_source_init(&gs, GREP_SOURCE_FILE, buf.buf, filename, filename);
362 + grep_source_init_file(&gs, buf.buf, filename);
363 strbuf_release(&buf);
364
365 if (num_threads > 1) {
grep.c
+28 -18
@@ -1825,14 +1825,24 @@ int grep_source(struct grep_opt *opt, struct grep_source *gs)
1825 return grep_source_1(opt, gs, 0);
1826 }
1827
1828 +static void grep_source_init_buf(struct grep_source *gs, char *buf,
1829 + unsigned long size)
1830 +{
1831 + gs->type = GREP_SOURCE_BUF;
1832 + gs->name = NULL;
1833 + gs->path = NULL;
1834 + gs->buf = buf;
1835 + gs->size = size;
1836 + gs->driver = NULL;
1837 + gs->identifier = NULL;
1838 +}
1839 +
1840 int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size)
1841 {
1842 struct grep_source gs;
1843 int r;
1844
1833 - grep_source_init(&gs, GREP_SOURCE_BUF, NULL, NULL, NULL);
1834 - gs.buf = buf;
1835 - gs.size = size;
1845 + grep_source_init_buf(&gs, buf, size);
1846
1847 r = grep_source(opt, &gs);
1848
@@ -1840,28 +1850,28 @@ int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size)
1850 return r;
1851 }
1852
1843 -void grep_source_init(struct grep_source *gs, enum grep_source_type type,
1844 - const char *name, const char *path,
1845 - const void *identifier)
1853 +void grep_source_init_file(struct grep_source *gs, const char *name,
1854 + const char *path)
1855 {
1847 - gs->type = type;
1856 + gs->type = GREP_SOURCE_FILE;
1857 gs->name = xstrdup_or_null(name);
1858 gs->path = xstrdup_or_null(path);
1859 gs->buf = NULL;
1860 gs->size = 0;
1861 gs->driver = NULL;
1862 + gs->identifier = xstrdup(path);
1863 +}
1864
1854 - switch (type) {
1855 - case GREP_SOURCE_FILE:
1856 - gs->identifier = xstrdup(identifier);
1857 - break;
1858 - case GREP_SOURCE_OID:
1859 - gs->identifier = oiddup(identifier);
1860 - break;
1861 - case GREP_SOURCE_BUF:
1862 - gs->identifier = NULL;
1863 - break;
1864 - }
1865 +void grep_source_init_oid(struct grep_source *gs, const char *name,
1866 + const char *path, const struct object_id *oid)
1867 +{
1868 + gs->type = GREP_SOURCE_OID;
1869 + gs->name = xstrdup_or_null(name);
1870 + gs->path = xstrdup_or_null(path);
1871 + gs->buf = NULL;
1872 + gs->size = 0;
1873 + gs->driver = NULL;
1874 + gs->identifier = oiddup(oid);
1875 }
1876
1877 void grep_source_clear(struct grep_source *gs)
grep.h
+4 -3
@@ -195,9 +195,10 @@ struct grep_source {
195 struct userdiff_driver *driver;
196 };
197
198 -void grep_source_init(struct grep_source *gs, enum grep_source_type type,
199 - const char *name, const char *path,
200 - const void *identifier);
198 +void grep_source_init_file(struct grep_source *gs, const char *name,
199 + const char *path);
200 +void grep_source_init_oid(struct grep_source *gs, const char *name,
201 + const char *path, const struct object_id *oid);
202 void grep_source_clear_data(struct grep_source *gs);
203 void grep_source_clear(struct grep_source *gs);
204 void grep_source_load_driver(struct grep_source *gs,