Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions changelog/includeimports.dd
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
Automatically include imports via -i command line option
Added the -i command line option to automatically include imports

Added the command line option -i which causes the compiler to treat imported modules as if they were given on the command line. The option also accepts "module patterns" that include/exclude modules based on their name. For example, the following will include all modules whose names start with `foo`, except for those that start with `foo.bar`:

$(CONSOLE dmd -i=foo -i=-foo.bar)

Added the command line option -i which causes the compiler to treat imported modules as if they were given on the command line. The option also accepts "module patterns" that include/exclude modules based on their name. For example, the following will include all modules whose names start with "foo", except for those that start with "foo.bar":
---
dmd -i=foo,-foo.bar
---
The option -i by itself is equivalent to:
---
dmd -i=-std,-core,-etc,-object
---

$(CONSOLE dmd -i=-std,-core,-etc,-object)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this syntax was removed with this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed, unless #7863 gets merged before we release

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shoot, missed that one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ZombineDev do you want to make a PR for that one or shall I? I think at this point it's gotta be merged to the stable branch

6 changes: 3 additions & 3 deletions src/dmd/cli.d
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,10 @@ dmd -cov -unittest myprog.d
"look for imports also in directory"
),
Option("i",
"same as -i=-std,-core,-etc,-object"
"include imported modules except from druntime/phobos (equivalent to -i=-std -i=-core -i=-etc -i=-object)"
),
Option("i=[-]<pattern>,[-]<pattern>,...",
"include/exclude imported modules whose name matches one of <pattern>"
Option("i=[-]<pattern>",
"include (or exclude if prefixed with '-') imported modules whose names match <pattern>"
),
Option("ignore",
"ignore unsupported pragmas"
Expand Down
51 changes: 15 additions & 36 deletions src/dmd/mars.d
Original file line number Diff line number Diff line change
Expand Up @@ -1884,32 +1884,21 @@ private bool parseCommandLine(const ref Strings arguments, const size_t argc, re
params.useInline = true;
params.hdrStripPlainFunctions = false;
}
else if (p[1] == 'i')
else if (arg == "-i")
includeImports = true;
else if (startsWith(p + 1, "i="))
{
includeImports = true;
if (p[2] != '\0')
if (!p[3])
{
size_t start = 2 + (p[2] == '=');
for (size_t nameIndex = start;; nameIndex++)
{
if (endOfModulePattern(p[nameIndex]))
{
auto pattern = p + start;
// NOTE: we could check that the pattern only contains valid package characters
// if they aren't valid it's not a problem but would give the user a nice error message
if (start == nameIndex)
{
error("invalid option '%s', module patterns cannot be empty", p);
break;
}
includeModulePatterns.push(pattern);
if (p[nameIndex] == '\0')
{
break;
}
start = nameIndex + 1;
}
}
error("invalid option '%s', module patterns cannot be empty", p);
}
else
{
// NOTE: we could check that the argument only contains valid "module-pattern" characters.
// Invalid characters doesn't break anything but an error message to the user might
// be nice.
includeModulePatterns.push(p + 3);
}
}
else if (arg == "-dip25") // https://dlang.org/dmd.html#switch-dip25
Expand Down Expand Up @@ -2350,16 +2339,6 @@ private void createMatchNodes()
}
}

/*
* Module patterns are taken directly from the -i command line option,
* so they can either end with the traditional terminating null, or they
* can end with a comma. This function returns true if the given character
* matches either of these.
* Returns:
* True if the given character is '\0' or ','.
*/
private bool endOfModulePattern(char c) { return c == ',' || c == '\0'; }

/*
* Determines the depth of the given module pattern.
* Params:
Expand All @@ -2373,7 +2352,7 @@ private ushort parseModulePatternDepth(const(char)* modulePattern)
modulePattern++;

// handle special case
if (modulePattern[0] == '.' && endOfModulePattern(modulePattern[1]))
if (modulePattern[0] == '.' && modulePattern[1] == '\0')
return 0;

ushort depth = 1;
Expand All @@ -2382,7 +2361,7 @@ private ushort parseModulePatternDepth(const(char)* modulePattern)
auto c = *modulePattern;
if (c == '.')
depth++;
if (endOfModulePattern(c))
if (c == '\0')
return depth;
}
}
Expand Down Expand Up @@ -2437,7 +2416,7 @@ private void parseModulePattern(const(char)* modulePattern, MatcherNode* dst, us
}
for (;; modulePattern++)
{
if (endOfModulePattern(*modulePattern))
if (*modulePattern == '\0')
{
assert(modulePattern > idStart, "empty module pattern");
*lastNode = MatcherNode(Identifier.idPool(idStart, modulePattern - idStart));
Expand Down
1 change: 0 additions & 1 deletion test/compilable/needsmod.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// ARG_SETS: -i=.
// ARG_SETS: -i=imports
// ARG_SETS: -i=imports.foofunc
// ARG_SETS: -iimports.foofunc
// PERMUTE_ARGS:
// LINK:
import imports.foofunc;
Expand Down
9 changes: 9 additions & 0 deletions test/fail_compilation/emptyModulePattern.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
REQUIRED_ARGS: -i=
PERMUTE_ARGS:
TEST_OUTPUT:
---
Error: invalid option '-i=', module patterns cannot be empty
run 'dmd -man' to open browser on manual
---
*/