Skip to content

Instantly share code, notes, and snippets.

@andrew-d
Created October 28, 2018 08:02
Show Gist options
  • Save andrew-d/88296648d07774f97808e5f7a13b1282 to your computer and use it in GitHub Desktop.
Save andrew-d/88296648d07774f97808e5f7a13b1282 to your computer and use it in GitHub Desktop.
Testing patch for `cpp` in Nixpkgs that removes store paths from the __FILE__ macro
diff --git i/libcpp/macro.c w/libcpp/macro.c
index c2515534504..ab593251def 100644
--- i/libcpp/macro.c
+++ w/libcpp/macro.c
@@ -288,8 +288,8 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node,
case BT_FILE:
case BT_BASE_FILE:
{
- unsigned int len;
- const char *name;
+ unsigned int len, storelen, i;
+ const char *name, *nix_store;
uchar *buf;
if (node->value.builtin == BT_FILE)
@@ -302,9 +302,34 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node,
abort ();
}
len = strlen (name);
+
buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);
result = buf;
*buf = '"';
+
+ /* If the name starts with the Nix store path, then replace it with a
+ fake path. Nix store paths look like this:
+ $NIX_STORE/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-foobar
+
+ So, if the string is longer than strlen("$NIX_STORE") + 33
+ characters, we replace the middle hash with the fake, all-`e` hash.
+ */
+ nix_store = getenv ("NIX_STORE");
+ if (nix_store)
+ {
+ storelen = strlen (nix_store);
+ if (len > storelen + 33 &&
+ strncmp (nix_store, name, strlen(nix_store)) == 0)
+ {
+ buf = cpp_quote_string (buf + 1, (const unsigned char *) nix_store, storelen);
+ *buf++ = '/';
+ for (i = 0; i < 32; i++)
+ *buf++ = 'e';
+
+ name += storelen + 33;
+ }
+ }
+
buf = cpp_quote_string (buf + 1, (const unsigned char *) name, len);
*buf++ = '"';
*buf = '\0';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment