Skip to content

Commit b2bf305

Browse files
authored
Merge pull request #2176 from wilzbach/trailing-parentheses
Fix trailing parenthesis + add test merged-on-behalf-of: Vladimir Panteleev <github@thecybershadow.net>
2 parents d276aad + d100a92 commit b2bf305

File tree

3 files changed

+87
-10
lines changed

3 files changed

+87
-10
lines changed

articles/cpptod.dd

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ See also: <a href="ctod.html">Programming in D for C Programmers</a>
1414

1515
$(HEADERNAV_TOC)
1616

17-
)
18-
19-
2017
<hr>$(COMMENT -------------------------------------------- )
2118

2219
$(H2 $(LNAME2 constructors, Defining constructors))
@@ -903,7 +900,7 @@ SecondVariant.method();
903900

904901
<hr>$(COMMENT -------------------------------------------- )
905902

906-
$(H3 $(LNAME2 references, References in D))
903+
$(H2 $(LNAME2 references, References in D))
907904

908905

909906
D doesn't have a (C++-style) concept of references as part of the type. Arguments can be passed by reference - hence the `ref` keyword, but "free" references don't exist in the language.

check_ddoc.d

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env rdmd
2+
/**
3+
Search HTML output for errors:
4+
- for undefined Ddoc macros
5+
- raw macro leakage
6+
- trailing parenthesis
7+
*/
8+
import std.algorithm, std.file, std.functional, std.range, std.stdio;
9+
10+
shared int errors = 0;
11+
12+
void error(string name, string file, size_t lineNr, const(char)[] line)
13+
{
14+
import core.atomic;
15+
errors.atomicOp!"+="(1);
16+
synchronized
17+
{
18+
stderr.writefln("%s:%d: [%s] %s", file, lineNr, name, line);
19+
}
20+
}
21+
22+
enum ErrorMessages
23+
{
24+
undefinedMacro = "UNDEFINED MACRO",
25+
rawMacroLeakage = "RAW MACRO LEAKAGE",
26+
trailingParenthesis = "TRAILING PARENTHESIS",
27+
}
28+
29+
void checkLine(alias errorFun)(string file, size_t lineNr, const(char)[] line)
30+
{
31+
if (line.canFind("UNDEFINED MACRO"))
32+
errorFun(ErrorMessages.undefinedMacro, file, lineNr, line);
33+
34+
if (line.findSplitAfter("$(")
35+
.pipe!(a => !a.expand.only.any!empty && a[1].front != '\''))
36+
errorFun(ErrorMessages.rawMacroLeakage, file, lineNr, line);
37+
38+
if (line.equal(")"))
39+
errorFun(ErrorMessages.trailingParenthesis, file, lineNr, line);
40+
}
41+
42+
version(unittest) {} else
43+
int main(string[] args)
44+
{
45+
import std.parallelism : parallel;
46+
47+
auto files = args[1 .. $];
48+
foreach (file; files.parallel(1))
49+
foreach (nr, line; File(file, "r").byLine.enumerate)
50+
checkLine!error(file, nr, line);
51+
52+
if (errors > 0)
53+
stderr.writefln("%s error%s found. Exiting.", errors, errors > 1 ? "s" : "");
54+
55+
return errors != 0;
56+
}
57+
58+
unittest
59+
{
60+
string lastSeenError;
61+
void errorStub(string name, string file, size_t lineNr, const(char)[] line)
62+
{
63+
lastSeenError = name;
64+
}
65+
auto check(string line)
66+
{
67+
lastSeenError = null;
68+
checkLine!errorStub(null, 0, line);
69+
return lastSeenError;
70+
}
71+
72+
assert(check(` <!--UNDEFINED MACRO: "D_CONTRIBUTORS"--> `) == ErrorMessages.undefinedMacro);
73+
74+
assert(check(" $('") is null);
75+
assert(check(" $(") is null);
76+
assert(check(" $(FOO)") == ErrorMessages.rawMacroLeakage);
77+
78+
assert(check(" )") is null);
79+
assert(check(") ") is null);
80+
assert(check(")") == ErrorMessages.trailingParenthesis);
81+
}

posix.mak

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ $W/spec/%.html : spec/%.dd $(SPEC_DDOC) $(DMD) $(DDOC_BIN)
479479
$W/404.html : 404.dd $(DDOC) $(DMD)
480480
$(DMD) -conf= -c -o- -Df$@ $(DDOC) errorpage.ddoc $<
481481

482-
$(DOC_OUTPUT_DIR)/contributors.html: contributors.dd $G/contributors_list.ddoc $(DDOC) $(DMD)
482+
$(DOC_OUTPUT_DIR)/foundation/contributors.html: foundation/contributors.dd $G/contributors_list.ddoc $(DDOC) $(DMD)
483483
$(DMD) -conf= -c -o- -Df$@ $(DDOC) $(word 2, $^) $<
484484

485485
$W/articles/%.html : articles/%.dd $(DDOC) $(DMD) $(DDOC_BIN) articles/articles.ddoc
@@ -886,15 +886,14 @@ test_dspec: dspec_tester.d $(DMD) $(PHOBOS_LIB)
886886
$(DMD) -run $< --compiler=$(DMD)
887887

888888
.PHONY:
889-
test: $(ASSERT_WRITELN_BIN)_test test_dspec test/next_version.sh all
889+
test: $(ASSERT_WRITELN_BIN)_test test_dspec test/next_version.sh all | $(STABLE_DMD)
890890
@echo "Searching for trailing whitespace"
891891
@grep -n '[[:blank:]]$$' $$(find . -type f -name "*.dd" | grep -v .generated) ; test $$? -eq 1
892892
@echo "Searching for tabs"
893893
@grep -n -P "\t" $$(find . -type f -name "*.dd" | grep -v .generated) ; test $$? -eq 1
894-
@echo "Searching for undefined macros"
895-
@grep -n "UNDEFINED MACRO" $$(find $W -type f -name "*.html" -not -path "$W/phobos/*") ; test $$? -eq 1
896-
@echo "Searching for undefined ddoc"
897-
@grep -n "[\$$]([^']" $$(find $W -type f -name "*.html" -not -path "$W/phobos/*") ; test $$? -eq 1
894+
@echo "Checking DDoc's output"
895+
$(STABLE_RDMD) -main -unittest check_ddoc.d
896+
$(STABLE_RDMD) check_ddoc.d $$(find $W -type f -name "*.html" -not -path "$W/phobos/*")
898897
@echo "Executing assert_writeln_magic tests"
899898
$<
900899
@echo "Executing next_version tests"

0 commit comments

Comments
 (0)