blob: 6e964dd337bffc6193def21c089073c6725f93df [file] [log] [blame]
John Thompson2d2d45e2013-09-20 14:40:52 +00001.. index:: modularize
2
3==================================
4Modularize User's Manual
5==================================
6
7.. toctree::
8 :hidden:
9
10 ModularizeUsage
11
12:program:`modularize` is a standalone tool that checks whether a set of headers
13provides the consistent definitions required to use modules. For example, it
14detects whether the same entity (say, a NULL macro or size_t typedef) is
15defined in multiple headers or whether a header produces different definitions
16under different circumstances. These conditions cause modules built from the
17headers to behave poorly, and should be fixed before introducing a module
18map.
19
John Thompson26ecaf92013-10-16 13:44:21 +000020:program:`modularize` also has an assistant mode option for generating
21a module map file based on the provided header list. The generated file
22is a functional module map that can be used as a starting point for a
23module.map file.
24
John Thompson2d2d45e2013-09-20 14:40:52 +000025Getting Started
26===============
27
28To build from source:
29
301. Read `Getting Started with the LLVM System`_ and `Clang Tools
31 Documentation`_ for information on getting sources for LLVM, Clang, and
32 Clang Extra Tools.
33
342. `Getting Started with the LLVM System`_ and `Building LLVM with CMake`_ give
35 directions for how to build. With sources all checked out into the
36 right place the LLVM build will build Clang Extra Tools and their
37 dependencies automatically.
38
39 * If using CMake, you can also use the ``modularize`` target to build
40 just the modularize tool and its dependencies.
41
42Before continuing, take a look at :doc:`ModularizeUsage` to see how to invoke
43modularize.
44
45.. _Getting Started with the LLVM System: http://llvm.org/docs/GettingStarted.html
46.. _Building LLVM with CMake: http://llvm.org/docs/CMake.html
47.. _Clang Tools Documentation: http://clang.llvm.org/docs/ClangTools.html
48
49What Modularize Checks
50======================
51
John Thompson587f6db2013-09-20 16:47:33 +000052Modularize will check for the following:
53
54* Duplicate global type and variable definitions
55* Duplicate macro definitions
56* Macro instances, 'defined(macro)', or #if, #elif, #ifdef, #ifndef conditions
57 that evaluate differently in a header
58* #include directives inside 'extern "C/C++" {}' or 'namespace (name) {}' blocks
59
John Thompson2d2d45e2013-09-20 14:40:52 +000060Modularize will do normal C/C++ parsing, reporting normal errors and warnings,
John Thompson8925d462013-09-23 14:17:27 +000061but will also report special error messages like the following::
John Thompson2d2d45e2013-09-20 14:40:52 +000062
John Thompson8925d462013-09-23 14:17:27 +000063 error: '(symbol)' defined at multiple locations:
64 (file):(row):(column)
65 (file):(row):(column)
John Thompson2d2d45e2013-09-20 14:40:52 +000066
John Thompson8925d462013-09-23 14:17:27 +000067 error: header '(file)' has different contents depending on how it was included
John Thompson2d2d45e2013-09-20 14:40:52 +000068
John Thompson8925d462013-09-23 14:17:27 +000069The latter might be followed by messages like the following::
John Thompson2d2d45e2013-09-20 14:40:52 +000070
John Thompson8925d462013-09-23 14:17:27 +000071 note: '(symbol)' in (file) at (row):(column) not always provided
John Thompson2d2d45e2013-09-20 14:40:52 +000072
73Checks will also be performed for macro expansions, defined(macro)
74expressions, and preprocessor conditional directives that evaluate
John Thompson8925d462013-09-23 14:17:27 +000075inconsistently, and can produce error messages like the following::
John Thompson2d2d45e2013-09-20 14:40:52 +000076
John Thompson8925d462013-09-23 14:17:27 +000077 (...)/SubHeader.h:11:5:
78 #if SYMBOL == 1
79 ^
80 error: Macro instance 'SYMBOL' has different values in this header,
81 depending on how it was included.
82 'SYMBOL' expanded to: '1' with respect to these inclusion paths:
83 (...)/Header1.h
84 (...)/SubHeader.h
85 (...)/SubHeader.h:3:9:
86 #define SYMBOL 1
87 ^
88 Macro defined here.
89 'SYMBOL' expanded to: '2' with respect to these inclusion paths:
90 (...)/Header2.h
91 (...)/SubHeader.h
92 (...)/SubHeader.h:7:9:
93 #define SYMBOL 2
94 ^
95 Macro defined here.
John Thompson2d2d45e2013-09-20 14:40:52 +000096
97Checks will also be performed for '#include' directives that are
98nested inside 'extern "C/C++" {}' or 'namespace (name) {}' blocks,
John Thompson8925d462013-09-23 14:17:27 +000099and can produce error message like the following::
John Thompson2d2d45e2013-09-20 14:40:52 +0000100
John Thompson8925d462013-09-23 14:17:27 +0000101 IncludeInExtern.h:2:3:
102 #include "Empty.h"
103 ^
104 error: Include directive within extern "C" {}.
105 IncludeInExtern.h:1:1:
106 extern "C" {
107 ^
108 The "extern "C" {}" block is here.
John Thompson26ecaf92013-10-16 13:44:21 +0000109
110.. _module-map-generation:
111
112Module Map Generation
113=====================
114
115If you specify the ``-module-map-path=<module map file>``,
116:program:`modularize` will output a module map based on the input header list.
117A module will be created for each header. Also, if the header in the header
118list is a partial path, a nested module hierarchy will be created in which a
119module will be created for each subdirectory component in the header path,
120with the header itself represented by the innermost module. If other headers
121use the same subdirectories, they will be enclosed in these same modules also.
122
123For example, for the header list::
124
125 SomeTypes.h
126 SomeDecls.h
127 SubModule1/Header1.h
128 SubModule1/Header2.h
129 SubModule2/Header3.h
130 SubModule2/Header4.h
131 SubModule2.h
132
133The following module map will be generated::
134
135 // Output/NoProblemsAssistant.txt
136 // Generated by: modularize -module-map-path=Output/NoProblemsAssistant.txt \
137 -root-module=Root NoProblemsAssistant.modularize
138
139 module SomeTypes {
140 header "SomeTypes.h"
141 export *
142 }
143 module SomeDecls {
144 header "SomeDecls.h"
145 export *
146 }
147 module SubModule1 {
148 module Header1 {
149 header "SubModule1/Header1.h"
150 export *
151 }
152 module Header2 {
153 header "SubModule1/Header2.h"
154 export *
155 }
156 }
157 module SubModule2 {
158 module Header3 {
159 header "SubModule2/Header3.h"
160 export *
161 }
162 module Header4 {
163 header "SubModule2/Header4.h"
164 export *
165 }
166 header "SubModule2.h"
167 export *
168 }
169
170An optional ``-root-module=<root-name>`` option can be used to cause a root module
171to be created which encloses all the modules.
172
173For example, with the same header list from above::
174
175 // Output/NoProblemsAssistant.txt
176 // Generated by: modularize -module-map-path=Output/NoProblemsAssistant.txt \
177 -root-module=Root NoProblemsAssistant.modularize
178
179 module Root {
180 module SomeTypes {
181 header "SomeTypes.h"
182 export *
183 }
184 module SomeDecls {
185 header "SomeDecls.h"
186 export *
187 }
188 module SubModule1 {
189 module Header1 {
190 header "SubModule1/Header1.h"
191 export *
192 }
193 module Header2 {
194 header "SubModule1/Header2.h"
195 export *
196 }
197 }
198 module SubModule2 {
199 module Header3 {
200 header "SubModule2/Header3.h"
201 export *
202 }
203 module Header4 {
204 header "SubModule2/Header4.h"
205 export *
206 }
207 header "SubModule2.h"
208 export *
209 }
210 }
211
212Note that headers with dependents will be ignored with a warning, as the
213Clang module mechanism doesn't support headers the rely on other headers
214to be included first.
215
216The module map format defines some keywords which can't be used in module
217names. If a header has one of these names, an underscore ('_') will be
218prepended to the name. For example, if the header name is ``header.h``,
219because ``header`` is a keyword, the module name will be ``_header``.
220For a list of the module map keywords, please see:
221`Lexical structure <http://clang.llvm.org/docs/Modules.html#lexical-structure>`_