Skip to content

Commit 25a0524

Browse files
committed
Add option to reorder declarations in output
1 parent 76fe008 commit 25a0524

File tree

6 files changed

+140
-1
lines changed

6 files changed

+140
-1
lines changed

src/cppconv/dwriter/declarationselection.d

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,28 @@ bool includeDeclsForFile(DWriterData data, string filename)
547547
|| data.options.includeDeclFilenamePatterns.match(filename);
548548
}
549549

550+
long getDeclarationOrder(Declaration d, DWriterData data)
551+
{
552+
foreach_reverse (ref pattern; data.options.declarationOrder)
553+
{
554+
DeclarationMatch match;
555+
if (isDeclarationMatch(pattern.match, match, d, data.semantic))
556+
{
557+
return pattern.order;
558+
}
559+
}
560+
return 0;
561+
}
562+
563+
bool cmpDeclarationLoc2(Declaration a, Declaration b, DWriterData data)
564+
{
565+
long orderA = getDeclarationOrder(a, data);
566+
long orderB = getDeclarationOrder(b, data);
567+
if (orderA != orderB)
568+
return orderA < orderB;
569+
return cmpDeclarationLoc(a, b, data.semantic);
570+
}
571+
550572
void selectDeclarations(DWriterData data)
551573
{
552574
auto semantic = data.semantic;
@@ -805,7 +827,7 @@ void selectDeclarations(DWriterData data)
805827

806828
foreach (name, ref decls2; data.declsByFile)
807829
{
808-
decls2.sort!((a, b) => cmpDeclarationLoc(a, b, semantic));
830+
decls2.sort!((a, b) => cmpDeclarationLoc2(a, b, data));
809831

810832
ImportInfo[string] neededImports;
811833
bool[string] neededPackages;

src/cppconv/dwriter/dwriter.d

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ struct FileHeaderReplacement
119119
ConfigRegexMultiline expectedLines;
120120
}
121121

122+
struct DeclarationOrderPattern
123+
{
124+
DeclarationPattern match;
125+
long order;
126+
}
127+
122128
struct DCodeOptions
123129
{
124130
DeclarationPattern[] blacklist;
@@ -143,6 +149,7 @@ struct DCodeOptions
143149
FileHeaderReplacement[] fileHeaderReplacement;
144150
ConfigRegex allowParameterImplicitCastsFilenamePatterns;
145151
ConfigRegex transitiveConstFilenamePatterns;
152+
DeclarationOrderPattern[] declarationOrder;
146153
}
147154

148155
class DWriterData
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"includeDeclFilenamePatterns": ".*",
3+
"declarationOrder": [
4+
{"match": {"name": "func1"}, "order": -1}
5+
]
6+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#undef TESTINCLUDE10_H
2+
#include "testinclude110.h"
3+
4+
// Source comment for func1
5+
void func1()
6+
{
7+
}
8+
9+
// Source comment for constructor
10+
C::C()
11+
{
12+
}
13+
14+
// Source comment for destructor
15+
C::~C()
16+
{
17+
}
18+
19+
// Source comment for memberfunc
20+
int C::memberfunc()
21+
{
22+
return 2;
23+
}
24+
25+
// Source comment for func2
26+
void func2()
27+
{
28+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module testinclude110;
2+
3+
import config;
4+
import cppconvhelpers;
5+
6+
// Header comment for func1
7+
/+ void func1(); +/
8+
9+
// Source comment for func1
10+
void func1()
11+
{
12+
}
13+
14+
// Header comment for C
15+
extern(C++, class) struct C
16+
{
17+
public:
18+
// Header comment for constructor
19+
// Source comment for constructor
20+
@disable this();
21+
/+this()
22+
{
23+
}+/
24+
25+
// Header comment for destructor
26+
// Source comment for destructor
27+
~this()
28+
{
29+
}
30+
31+
// Header comment for inlinefunc
32+
int inlinefunc() {return 1;}
33+
34+
// Header comment for memberfunc
35+
// Source comment for memberfunc
36+
int memberfunc()
37+
{
38+
return 2;
39+
}
40+
}
41+
42+
// Header comment for func2
43+
/+ void func2();
44+
#undef TESTINCLUDE10_H +/
45+
// Source comment for func2
46+
void func2()
47+
{
48+
}
49+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef TESTINCLUDE10_H
2+
#define TESTINCLUDE10_H
3+
4+
// Header comment for func1
5+
void func1();
6+
7+
// Header comment for C
8+
class C
9+
{
10+
public:
11+
// Header comment for constructor
12+
C();
13+
14+
// Header comment for destructor
15+
~C();
16+
17+
// Header comment for inlinefunc
18+
int inlinefunc() {return 1;}
19+
20+
// Header comment for memberfunc
21+
int memberfunc();
22+
};
23+
24+
// Header comment for func2
25+
void func2();
26+
27+
#endif

0 commit comments

Comments
 (0)