summaryrefslogtreecommitdiffstats
path: root/rtemsspec/content.py
blob: 3a25b7ec16d1dfbfdc60d72c6ab1b4a1b3d837e3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
# SPDX-License-Identifier: BSD-2-Clause
""" This module provides classes for content generation. """

# Copyright (C) 2019, 2020 embedded brains GmbH (http://www.embedded-brains.de)
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# pylint: disable=too-many-lines

from contextlib import contextmanager
import collections
import itertools
import os
import re
import sys
import textwrap
from typing import Any, Callable, ContextManager, Dict, Iterable, Iterator, \
    List, NamedTuple, Optional, Set, Tuple, Union

from rtemsspec.items import Item, ItemGetValueContext

AddContext = Callable[["Content"], ContextManager[None]]
GenericContent = Union[str, List[str], "Content"]
GenericContentIterable = Union[Iterable[str], Iterable[List[str]],
                               Iterable[GenericContent]]


class Copyright:
    """
    This class represents a copyright holder with its years of substantial
    contributions.
    """
    def __init__(self, holder):
        self._holder = holder
        self._years = set()

    def add_year(self, year: str):
        """
        Adds a year to the set of substantial contributions of this copyright
        holder.
        """
        self._years.add(year)

    def get_statement(self) -> str:
        """ Returns a copyright statement. """
        line = "Copyright (C)"
        years = sorted(self._years)
        year_count = len(years)
        if year_count == 1:
            line += " " + years[0]
        elif year_count > 1:
            line += " " + years[0] + ", " + years[-1]
        line += " " + self._holder
        return line

    def __lt__(self, other: "Copyright") -> bool:
        # pylint: disable=protected-access
        if self._years and other._years:
            self_first_year = sorted(self._years)[0]
            other_first_year = sorted(other._years)[0]
            if self_first_year == other_first_year:
                return self._holder > other._holder
            return self_first_year > other_first_year
        if self._years or other._years:
            return True
        return self._holder > other._holder


class Copyrights:
    """ This class represents a set of copyright holders. """
    def __init__(self):
        self.copyrights = {}

    def register(self, statement):
        """ Registers a copyright statement. """
        match = re.search(
            r"^\s*Copyright\s+\(C\)\s+([0-9]+),\s*([0-9]+)\s+(.+)\s*$",
            statement,
            flags=re.I,
        )
        if match:
            holder = match.group(3)
            the_copyright = self.copyrights.setdefault(holder,
                                                       Copyright(holder))
            the_copyright.add_year(match.group(1))
            the_copyright.add_year(match.group(2))
            return
        match = re.search(
            r"^\s*Copyright\s+\(C\)\s+([0-9]+)\s+(.+)\s*$",
            statement,
            flags=re.I,
        )
        if match:
            holder = match.group(2)
            the_copyright = self.copyrights.setdefault(holder,
                                                       Copyright(holder))
            the_copyright.add_year(match.group(1))
            return
        match = re.search(r"^\s*Copyright\s+\(C\)\s+(.+)\s*$",
                          statement,
                          flags=re.I)
        if match:
            holder = match.group(1)
            self.copyrights.setdefault(holder, Copyright(holder))
            return
        raise ValueError(statement)

    def get_statements(self):
        """ Returns all registered copyright statements as a sorted list. """
        statements = []
        for the_copyright in sorted(self.copyrights.values()):
            statements.append(the_copyright.get_statement())
        return statements


def make_lines(content: GenericContent) -> List[str]:
    """ Makes a list of lines from a generic content. """
    if isinstance(content, str):
        return content.strip("\n").split("\n")
    if isinstance(content, list):
        return content
    return content.lines


def _indent(lines: List[str], indent: str,
            empty_line_indent: str) -> List[str]:
    if indent:
        return [
            indent + line if line else empty_line_indent + line
            for line in lines
        ]
    return lines


@contextmanager
def _add_context(_content: "Content") -> Iterator[None]:
    yield


_SPECIAL_BLOCK = re.compile(r"^( *\* | *[0-9]+\. | +)")

_AUTOMATICALLY_GENERATED_WARNING = [
    "This file was automatically generated.  Do not edit it manually.",
    "Please have a look at",
    "",
    "https://docs.rtems.org/branches/master/eng/req/howto.html",
    "",
    "for information how to maintain and re-generate this file.",
]


class Content:
    """ This class builds content. """

    # pylint: disable=too-many-instance-attributes
    # pylint: disable=too-many-public-methods
    def __init__(self, the_license: str, pop_indent_gap: bool):
        self._lines = []  # type: List[str]
        self._license = the_license
        self._copyrights = Copyrights()
        self._gap = False
        self._tab = "  "
        self._indents = [""]
        self._indent = ""
        self._empty_line_indents = [""]
        self._empty_line_indent = ""
        self._pop_indent_gap = pop_indent_gap

    def __str__(self):
        return "\n".join(itertools.chain(self._lines, [""]))

    @property
    def lines(self) -> List[str]:
        """ The lines. """
        return self._lines

    @property
    def tab(self) -> str:
        """ The tabulator. """
        return self._tab

    def append(self, content: GenericContent) -> None:
        """ Appends the content. """
        self._lines.extend(
            _indent(make_lines(content), self._indent,
                    self._empty_line_indent))

    def prepend(self, content: GenericContent) -> None:
        """ Prepends the content. """
        self._lines[0:0] = _indent(make_lines(content), self._indent,
                                   self._empty_line_indent)

    def add(self,
            content: Optional[GenericContent],
            context: AddContext = _add_context) -> None:
        """
        Skips leading empty lines, adds a gap if needed, then adds the content.
        """
        if not content:
            return
        lines = make_lines(content)
        for index, line in enumerate(lines):
            if line:
                self._add_gap()
                with context(self):
                    self._lines.extend(
                        _indent(lines[index:], self._indent,
                                self._empty_line_indent))
                break

    def wrap_text(self,
                  text: str,
                  initial_indent: str = "",
                  subsequent_indent: Optional[str] = None,
                  context: AddContext = _add_context) -> None:
        """ Adds a gap if needed, then adds the wrapped text.  """
        self._add_gap()
        with context(self):
            if subsequent_indent is None:
                if initial_indent:
                    subsequent_indent = self._tab
                else:
                    subsequent_indent = ""
            wrapper = textwrap.TextWrapper()
            wrapper.break_long_words = False
            wrapper.break_on_hyphens = False
            wrapper.initial_indent = initial_indent
            wrapper.width = 79 - len(self._indent)
            gap = []  # type: List[str]
            for block in text.split("\n\n"):
                match = _SPECIAL_BLOCK.search(block)
                if match:
                    space = len(match.group(0)) * " "
                    wrapper.subsequent_indent = f"{subsequent_indent}{space}"
                    block = block.replace(f"\n{space}", "\n")
                else:
                    wrapper.subsequent_indent = subsequent_indent
                self._lines.extend(gap)
                self._lines.extend(
                    _indent(wrapper.wrap(block), self._indent,
                            self._empty_line_indent))
                gap = [self._empty_line_indent]

    def wrap(self,
             content: Optional[GenericContent],
             initial_indent: str = "",
             subsequent_indent: Optional[str] = None,
             context: AddContext = _add_context) -> None:
        """ Adds a gap if needed, then adds the wrapped content.  """
        if not content:
            return
        if isinstance(content, str):
            text = content
        elif isinstance(content, list):
            text = "\n".join(content)
        else:
            text = "\n".join(content.lines)
        text = text.strip()
        if not text:
            return
        self.wrap_text(text, initial_indent, subsequent_indent, context)

    def paste(self, content: Optional[GenericContent]) -> None:
        """ Pastes the wrapped content directly to the last line.  """
        if not content:
            return
        if isinstance(content, str):
            text = content
        elif isinstance(content, list):
            text = "\n".join(content)
        else:
            text = "\n".join(content.lines)
        indent_len = len(self._indent)
        try:
            last = self._lines[-1]
            text = last[indent_len:] + " " + text
        except IndexError:
            last = ""
        text = text.strip()
        if not text:
            return
        wrapper = textwrap.TextWrapper()
        wrapper.break_long_words = False
        wrapper.break_on_hyphens = False
        wrapper.initial_indent = ""
        wrapper.width = 79 - len(self._indent)
        for index, block in enumerate(text.split("\n\n")):
            if index == 0:
                wrapper.subsequent_indent = ""
                lines = wrapper.wrap(block)
                if 0 < len(last) >= indent_len:
                    self._lines[-1] = last[0:indent_len] + lines[0]
                    lines = lines[1:]
                self.gap = True
            else:
                match = _SPECIAL_BLOCK.search(block)
                if match:
                    space = len(match.group(0)) * " "
                    wrapper.subsequent_indent = space
                    block = block.replace(f"\n{space}", "\n")
                else:
                    wrapper.subsequent_indent = ""
                lines = wrapper.wrap(block)
                self._lines.append(self._empty_line_indent)
            self._lines.extend(
                _indent(lines, self._indent, self._empty_line_indent))

    def _add_gap(self) -> None:
        if self._gap:
            self._lines.extend(
                _indent([""], self._indent, self._empty_line_indent))
        self._gap = True

    @property
    def gap(self) -> bool:
        """
        True if the next Content.add() adds a gap before the new content,
        otherwise False.
        """
        return self._gap

    @gap.setter
    def gap(self, value: bool) -> None:
        """ Sets the gap indicator for Content.add(). """
        self._gap = value

    def _update_indent(self) -> None:
        self._indent = "".join(self._indents)
        empty_line_indent = "".join(self._empty_line_indents)
        if empty_line_indent.isspace():
            self._empty_line_indent = ""
        else:
            self._empty_line_indent = empty_line_indent

    def push_indent(self,
                    indent: Optional[str] = None,
                    empty_line_indent: Optional[str] = None) -> None:
        """ Pushes an indent level. """
        self._indents.append(indent if indent else self._tab)
        self._empty_line_indents.append(
            empty_line_indent if empty_line_indent else self._tab)
        self._update_indent()
        self.gap = False

    def pop_indent(self) -> None:
        """ Pops an indent level. """
        self._indents.pop()
        self._empty_line_indents.pop()
        self._update_indent()
        self.gap = self._pop_indent_gap

    @contextmanager
    def indent(self,
               indent: Optional[str] = None,
               empty_line_indent: Optional[str] = None) -> Iterator[None]:
        """ Opens an indent context. """
        self.push_indent(indent, empty_line_indent)
        yield
        self.pop_indent()

    def indent_lines(self, level: int) -> None:
        """ Indents all lines by the specified indent level. """
        prefix = level * self._tab
        self._lines = [prefix + line if line else line for line in self._lines]

    def add_blank_line(self):
        """ Adds a blank line. """
        self._lines.append("")

    def ensure_blank_line(self):
        """ Ensures that the last line is blank. """
        if not self._lines or self._lines[-1]:
            self._lines.append("")

    def register_license(self, the_license: str) -> None:
        """ Registers a licence for the content. """
        licenses = re.split(r"\s+OR\s+", the_license)
        if self._license not in licenses:
            raise ValueError(the_license)

    def register_copyright(self, statement: str) -> None:
        """ Registers a copyright statement for the content. """
        self._copyrights.register(statement)

    def register_license_and_copyrights_of_item(self, item: Item) -> None:
        """ Registers the license and copyrights of the item. """
        self.register_license(item["SPDX-License-Identifier"])
        for statement in item["copyrights"]:
            self.register_copyright(statement)

    def open_comment_block(self) -> None:
        """ Opens a comment block. """
        self.push_indent("# ", "#")

    def close_comment_block(self) -> None:
        """ Closes a comment block. """
        self.pop_indent()
        self.gap = True

    @contextmanager
    def comment_block(self) -> Iterator[None]:
        """ Opens a comment block context. """
        self.open_comment_block()
        yield
        self.close_comment_block()

    def add_automatically_generated_warning(self) -> None:
        """ Adds a warning that the file is automatically generated. """
        with self.comment_block():
            self.append(_AUTOMATICALLY_GENERATED_WARNING)

    def write(self, path: str) -> None:
        """ Writes the content to the file specified by the path. """
        directory = os.path.dirname(path)
        if directory:
            os.makedirs(directory, exist_ok=True)
        with open(path, "w+") as out:
            out.write(str(self))


_BSD_2_CLAUSE_LICENSE = """Redistribution and use in source and binary \
forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE."""

_PARAM = {
    None: "@param ",
    "in": "@param[in] ",
    "out": "@param[out] ",
    "inout": "@param[in,out] ",
}


class CInclude(NamedTuple):
    """ A C include file. """
    path: str
    enabled_by: str = ""


def _split_includes(
        includes: List[CInclude]) -> Tuple[Set[str], Dict[str, Set[str]]]:
    includes_unconditional = set()  # type: Set[str]
    includes_enabled_by = {}  # type: Dict[str, Set[str]]
    for inc in list(dict.fromkeys(includes)):
        if inc.enabled_by and inc.enabled_by != "1":
            try:
                includes_unconditional.remove(inc.path)
            except KeyError:
                pass
            includes_enabled_by.setdefault(inc.path, set()).add(inc.enabled_by)
        elif inc.path not in includes_enabled_by:
            includes_unconditional.add(inc.path)
    return includes_unconditional, includes_enabled_by


_FUNCTION_POINTER = re.compile(r"^[^(]+\(\s\*([^)]+)\)\s*\(")
_DESIGNATOR = re.compile(r"([a-zA-Z0-9_]+)$")


def _get_align_pos(param: str) -> Tuple[int, int]:
    if param == "...":
        return 0, sys.maxsize
    match = _DESIGNATOR.search(param)
    if not match:
        match = _FUNCTION_POINTER.search(param)
        assert match
    star = param.find("*")
    if star >= 0:
        return star, match.start(1)
    return match.start(1), match.start(1)


def _align_params(params: List[str]) -> List[str]:
    positions = list(map(_get_align_pos, params))
    max_pos = max(positions)[1]
    return [
        param[:pos[0]] + (max_pos - pos[1]) * " " + param[pos[0]:]
        for param, pos in zip(params, positions)
    ]


_NOT_ALPHANUM = re.compile(r"[^a-zA-Z0-9_]")
_SPHINX_FILE_TO_DOXYGEN = re.compile(r":file:`([^`]+)`")


class CContent(Content):
    """ This class builds C content. """

    # pylint: disable=too-many-public-methods
    def __init__(self):
        super().__init__("BSD-2-Clause", False)

    def doxyfy(self, content: Optional[GenericContent]) -> None:
        """
        Adds a gap if needed, then adds the wrapped text with some conversion
        to Doxygen markup.
        """
        if not content:
            return
        if isinstance(content, str):
            text = content
        elif isinstance(content, list):
            text = "\n".join(content)
        else:
            text = "\n".join(content.lines)
        text = text.strip()
        if not text:
            return
        blocks = collections.deque(text.split("\n\n"))
        while blocks:
            block = blocks.popleft()
            block = _SPHINX_FILE_TO_DOXYGEN.sub(
                lambda match: f"``{match.group(1)}``", block)
            if block.startswith(".. code-block"):
                self.add("@code")
                self.gap = False
                while blocks:
                    block = blocks.popleft()
                    if block.startswith("    "):
                        self.add(block[4:].replace("\n    ", "\n"))
                    else:
                        blocks.appendleft(block)
                        break
                self.append("@endcode")
            else:
                self.wrap_text(block)

    def prepend_spdx_license_identifier(self):
        """
        Adds an SPDX License Identifier according to the registered licenses.
        """
        self.prepend([f"/* SPDX-License-Identifier: {self._license} */", ""])

    def add_copyrights_and_licenses(self):
        """
        Adds the copyrights and licenses according to the registered copyrights
        and licenses.
        """
        with self.comment_block():
            self.add(self._copyrights.get_statements())
            self.add(_BSD_2_CLAUSE_LICENSE)

    def prepend_copyrights_and_licenses(self):
        """
        Prepends the copyrights and licenses according to the registered
        copyrights and licenses.
        """
        content = CContent()
        with content.comment_block():
            content.add(self._copyrights.get_statements())
            content.add(_BSD_2_CLAUSE_LICENSE)
        content.append("")
        self.prepend(content)

    def add_have_config(self):
        """ Adds a guarded config.h include. """
        self.add(["#ifdef HAVE_CONFIG_H", "#include \"config.h\"", "#endif"])

    def _add_includes(self, includes: Set[str], local: bool) -> None:
        class IncludeKey:  # pylint: disable=too-few-public-methods
            """ Provides a key to sort includes. """
            def __init__(self, inc: str):
                self._inc = inc

            def __lt__(self, other: "IncludeKey") -> bool:
                left = self._inc.split("/")
                right = other._inc.split("/")
                left_len = len(left)
                right_len = len(right)
                if left_len == right_len:
                    for left_part, right_part in zip(left[:-1], right[:-1]):
                        if left_part != right_part:
                            return left_part < right_part
                    return left[-1] < right[-1]
                return left_len < right_len

        left = "\"" if local else "<"
        right = "\"" if local else ">"
        self.add([
            f"#include {left}{inc}{right}"
            for inc in sorted(includes, key=IncludeKey)
        ])

    def _add_includes_enabled_by(self, includes: Dict[str, Set[str]],
                                 local: bool) -> None:
        enabled_by_includes = {}  # type: Dict[str, Set[str]]
        for inc, enabled_bys in iter(includes.items()):
            enabled_by_includes.setdefault(" && ".join(sorted(enabled_bys)),
                                           set()).add(inc)
        for enabled_by, incs in sorted(iter(enabled_by_includes.items())):
            self.add(f"#if {enabled_by}")
            with self.indent():
                self._add_includes(incs, local)
            self.add("#endif")

    def add_includes(self,
                     includes: List[CInclude],
                     local: bool = False) -> None:
        """ Adds a block of includes. """
        includes_unconditional, includes_enabled_by = _split_includes(includes)
        self._add_includes(includes_unconditional, local)
        self._add_includes_enabled_by(includes_enabled_by, local)

    def _open_comment_block(self, begin) -> None:
        self.add(begin)
        self.push_indent(" * ", " *")

    def open_comment_block(self) -> None:
        """ Opens a comment block. """
        self._open_comment_block("/*")

    def open_doxygen_block(self) -> None:
        """ Opens a Doxygen comment block. """
        self._open_comment_block("/**")

    def open_file_block(self) -> None:
        """ Opens a Doxygen @file comment block. """
        self._open_comment_block(["/**", " * @file"])
        self.gap = True

    def open_defgroup_block(self, identifier: str, name: str) -> None:
        """ Opens a Doxygen @defgroup comment block. """
        defgroup = [f" * @defgroup {identifier} {name}"]
        if len(self._indent) + len(defgroup[0]) > 79:
            defgroup = [f" * @defgroup {identifier} \\", f" *   {name}"]
        self._open_comment_block(["/**"] + defgroup)
        self.gap = True

    def open_function_block(self, function: str) -> None:
        """ Opens a Doxygen @fn comment block. """
        self._open_comment_block(["/**", f" * @fn {function}"])
        self.gap = True

    def close_comment_block(self) -> None:
        """ Closes a comment block. """
        self.pop_indent()
        self.append(" */")
        self.gap = True

    @contextmanager
    def doxygen_block(self) -> Iterator[None]:
        """ Opens a Doxygen comment block context. """
        self.open_doxygen_block()
        yield
        self.close_comment_block()

    @contextmanager
    def file_block(self) -> Iterator[None]:
        """ Opens a Doxygen @file comment block context. """
        self.open_file_block()
        yield
        self.close_comment_block()

    @contextmanager
    def defgroup_block(self, identifier: str, name: str) -> Iterator[None]:
        """ Opens a Doxygen @defgroup comment block context. """
        self.open_defgroup_block(identifier, name)
        yield
        self.close_comment_block()

    @contextmanager
    def function_block(self, function: str) -> Iterator[None]:
        """ Opens a Doxygen @fn comment block context. """
        self.open_function_block(function)
        yield
        self.close_comment_block()

    def open_add_to_group(self, group: str) -> None:
        """ Opens an add to group. """
        with self.doxygen_block():
            self.append([f"@addtogroup {group}", "", "@{"])

    def close_add_to_group(self) -> None:
        """ Closes an add to group. """
        self.add("/** @} */")

    @contextmanager
    def add_to_group(self, group: str) -> Iterator[None]:
        """ Opens an add to group context. """
        self.open_add_to_group(group)
        yield
        self.close_add_to_group()

    def open_for_loop(self, begin: str, end: str, step: str) -> None:
        """ Opens a for loop. """
        for_loop = [f"for ( {begin}; {end}; {step} ) {{"]
        if len(self._indent) + len(for_loop[0]) > 79:
            for_loop = [
                "for (", f"{self.tab}{begin};", f"{self.tab}{end};",
                f"{self.tab}{step}", ") {"
            ]
        self.add(for_loop)
        self.push_indent()

    def close_for_loop(self) -> None:
        """ Closes a for loop. """
        self.pop_indent()
        self.append(["}"])
        self.gap = True

    @contextmanager
    def for_loop(self, begin: str, end: str, step: str) -> Iterator[None]:
        """ Opens a for loop context. """
        self.open_for_loop(begin, end, step)
        yield
        self.close_for_loop()

    def _function(self, ret: str, name: str, params: List[str],
                  param_line: str, space: str, semicolon: str) -> None:
        # pylint: disable=too-many-arguments
        line = f"{ret}{space}{name}("
        if len(self._indent) + len(line) > 79:
            line = f"{name}{param_line}{semicolon}"
            if len(self._indent) + len(line) > 79:
                self.add([ret, f"{name}("])
            else:
                self.add([ret, line])
                return
        else:
            self.add(line)
        with self.indent():
            self.add(",\n".join(params))
        self.add(f"){semicolon}")

    def call_function(self,
                      ret: Optional[str],
                      name: str,
                      params: Optional[List[str]] = None) -> None:
        """ Adds a function call. """
        if ret:
            space = " "
        else:
            ret = ""
            space = ""
        if params:
            params = [param.strip() for param in params]
            param_line = "( " + ", ".join(params) + " )"
        else:
            param_line = "()"
        line = f"{ret}{space}{name}{param_line};"
        if len(self._indent) + len(line) > 79:
            if params:
                self._function(ret, name, params, param_line, space, ";")
            elif ret:
                self.add(ret)
                with self.indent():
                    self.add(f"{name}();")
            else:
                self.add(f"{name}();")
        else:
            self.add(line)

    def declare_function(self,
                         ret: str,
                         name: str,
                         params: Optional[List[str]] = None,
                         define: bool = False,
                         align: bool = True) -> None:
        # pylint: disable=too-many-arguments
        """ Adds a function declaration. """
        if params:
            params = [param.strip() for param in params]
        else:
            params = ["void"]
        param_line = f"( {', '.join(params)} )"
        space = "" if not ret or ret.endswith("*") else " "
        semicolon = "" if define else ";"
        line = f"{ret}{space}{name}{param_line}{semicolon}"
        if len(self._indent) + len(line) > 79:
            if align:
                params = _align_params(params)
            self._function(ret, name, params, param_line, space, semicolon)
        else:
            self.add(line)

    def open_function(self,
                      ret: str,
                      name: str,
                      params: Optional[List[str]] = None,
                      align: bool = True) -> None:
        """ Opens a function definition. """
        self.declare_function(ret, name, params, define=True, align=align)
        self.append("{")
        self.push_indent()

    def close_function(self) -> None:
        """ Closes a function definition. """
        self.pop_indent()
        self.add("}")

    @contextmanager
    def function(self,
                 ret: str,
                 name: str,
                 params: Optional[List[str]] = None,
                 align: bool = True) -> Iterator[None]:
        """ Opens a function context. """
        self.open_function(ret, name, params, align=align)
        yield
        self.close_function()

    def open_condition(self,
                       expression: Optional[str],
                       chain: bool = False) -> None:
        """ Opens a condition. """
        begin = "} else " if chain else ""
        ifelse = f"if ( {expression} ) " if expression else ""
        self.add(f"{begin}{ifelse}{{")
        self.push_indent()

    def close_condition(self) -> None:
        """ Closes a condition. """
        self.pop_indent()
        self.add("}")

    @contextmanager
    def condition(self, expression: Optional[str]) -> Iterator[None]:
        """ Opens a condition context. """
        self.open_condition(expression)
        yield
        self.close_condition()

    @contextmanager
    def first_condition(self, expression: Optional[str]) -> Iterator[None]:
        """ Opens the first condition context. """
        self.open_condition(expression, False)
        yield
        self.pop_indent()

    @contextmanager
    def next_condition(self, expression: Optional[str]) -> Iterator[None]:
        """ Opens the next condition context. """
        self.open_condition(expression, True)
        yield
        self.pop_indent()

    @contextmanager
    def final_condition(self, expression: Optional[str]) -> Iterator[None]:
        """ Opens the final condition context. """
        self.open_condition(expression, True)
        yield
        self.close_condition()

    def add_brief_description(self, description: Optional[str]) -> None:
        """ Adds a brief description. """
        self.wrap(description, initial_indent="@brief ")

    def add_param_description(
            self,
            params: Iterable[Dict[str, str]],
            substitute: Callable[[str], str] = lambda x: x) -> None:
        """ Adds a list of parameter descriptions. """
        for param in params:
            self.wrap(param["name"] + " " + substitute(param["description"]),
                      initial_indent=_PARAM[param["dir"]])

    def add_description_block(self, brief: Optional[str],
                              description: Optional[str]) -> None:
        """ Adds a description block. """
        if brief or description:
            with self.doxygen_block():
                self.add_brief_description(brief)
                self.wrap(description)
            self.gap = False

    def add_ingroup(self, ingroups: List[str]) -> None:
        """ Adds an ingroup comment block. """
        self.add(["@ingroup " + ingroup for ingroup in sorted(set(ingroups))])

    def add_group(self, identifier: str, name: str, ingroups: List[str],
                  brief: Optional[str], description: Optional[str]) -> None:
        # pylint: disable=too-many-arguments
        """ Adds a group definition. """
        with self.defgroup_block(identifier, name):
            self.add_ingroup(ingroups)
            self.add_brief_description(brief)
            self.wrap(description)

    @contextmanager
    def header_guard(self, filename: str) -> Iterator[None]:
        """ Opens a header guard context. """
        guard = "_" + _NOT_ALPHANUM.sub("_", filename).upper()
        self.add([f"#ifndef {guard}", f"#define {guard}"])
        yield
        self.add(f"#endif /* {guard} */")

    @contextmanager
    def extern_c(self) -> Iterator[None]:
        """ Opens an extern "C" context. """
        self.add(["#ifdef __cplusplus", "extern \"C\" {", "#endif"])
        yield
        self.add(["#ifdef __cplusplus", "}", "#endif"])

    def add_paragraph(self, name: str,
                      content: Optional[GenericContent]) -> None:
        """ Adds a Doxygen paragraph block. """
        if content:
            self.add(f"@par {name}")
            self.gap = False
            last = len(self.lines)
            self.doxyfy(content)
            if self._empty_line_indent in self.lines[last:]:
                self.lines.insert(last, f"{self._indent}@parblock")
                self.lines.append(f"{self._indent}@endparblock")


def get_value_doxygen_function(ctx: ItemGetValueContext) -> Any:
    """ Gets a value as a function for Doxygen markup. """
    return f"{ctx.value[ctx.key]}()"


def get_value_double_colon(ctx: ItemGetValueContext) -> Any:
    """ Gets a value with a :: prefix. """
    return f"::{ctx.value[ctx.key]}"


def get_value_hash(ctx: ItemGetValueContext) -> Any:
    """ Gets a value with a # prefix. """
    return f"#{ctx.value[ctx.key]}"


class ExpressionMapper:
    """ Maps symbols and operations to form a C expression. """

    # pylint: disable=no-self-use
    def map_bool(self, value: bool) -> str:
        """ Maps a boolean value to build an expression. """
        return str(int(value))

    # pylint: disable=no-self-use
    def map_symbol(self, symbol: str) -> str:
        """ Maps a symbol to build an expression. """
        return f"defined({symbol})"

    def op_and(self) -> str:
        """ Returns the and operator. """
        return " && "

    def op_or(self) -> str:
        """ Returns the or operator. """
        return " || "

    def op_not(self, symbol: str) -> str:
        """ Returns the negation of the symbol. """
        return f"!{symbol}"


class PythonExpressionMapper(ExpressionMapper):
    """ Maps symbols and operations to form a Python expression. """

    # pylint: disable=no-self-use
    def map_bool(self, value: bool) -> str:
        return str(value)

    # pylint: disable=no-self-use
    def map_symbol(self, symbol: str) -> str:
        return symbol

    def op_and(self) -> str:
        return " and "

    def op_or(self) -> str:
        return " or "

    def op_not(self, symbol: str) -> str:
        return f"not {symbol}"


def _to_expression_op(enabled_by: Any, mapper: ExpressionMapper,
                      operation: str) -> str:
    symbols = [
        _to_expression(next_enabled_by, mapper)
        for next_enabled_by in enabled_by
    ]
    if len(symbols) == 1:
        return symbols[0]
    return f"({operation.join(symbols)})"


def _to_expression_op_and(enabled_by: Any, mapper: ExpressionMapper) -> str:
    return _to_expression_op(enabled_by, mapper, mapper.op_and())


def _to_expression_op_not(enabled_by: Any, mapper: ExpressionMapper) -> str:
    return mapper.op_not(_to_expression(enabled_by, mapper))


def _to_expression_op_or(enabled_by: Any, mapper: ExpressionMapper) -> str:
    return _to_expression_op(enabled_by, mapper, mapper.op_or())


_TO_EXPRESSION_OP = {
    "and": _to_expression_op_and,
    "not": _to_expression_op_not,
    "or": _to_expression_op_or
}


def _to_expression(enabled_by: Any, mapper: ExpressionMapper) -> str:
    if isinstance(enabled_by, bool):
        return mapper.map_bool(enabled_by)
    if isinstance(enabled_by, list):
        return _to_expression_op_or(enabled_by, mapper)
    if isinstance(enabled_by, dict):
        if len(enabled_by) == 1:
            key = next(iter(enabled_by))
            return _TO_EXPRESSION_OP[key](enabled_by[key], mapper)
        raise ValueError
    return mapper.map_symbol(enabled_by)


def enabled_by_to_exp(enabled_by: Any, mapper: ExpressionMapper) -> str:
    """
    Returns an expression for an enabled-by attribute value.
    """
    exp = _to_expression(enabled_by, mapper)
    if exp.startswith("("):
        return exp[1:-1]
    return exp


_CAMEL_CASE_TO_UPPER = re.compile(r"\s+(.)")
_CAMEL_CASE_DISCARD = re.compile(r"[^ \t\n\r\f\va-zA-Z0-9]")


def to_camel_case(name: str) -> str:
    """ Returns the name in CamelCase. """
    return name[0].upper() + _CAMEL_CASE_TO_UPPER.sub(
        lambda match: match.group(1).upper(),
        _CAMEL_CASE_DISCARD.sub(" ", name[1:].replace("+", "X")))