[cfe-dev] How do I try out C++ modules with clang?

Sean Silva chisophugis at gmail.com
Fri Oct 3 13:18:11 PDT 2014


Currently there is no "dotted path" (e.g. import std.algorithm, or
whatever) import in C++; #include's are translated into it: try looking at
-E with modules enabled.

You currently have to do everything through module maps; listing your
header in a module map basically tells clang "I promise you this header
meets certain assumptions of modularity" (which are really fairly
reasonable assumptions; e.g. you aren't expecting a certain macro to be
defined somewhere else in the code). Module maps also describe a "dotted
import path", but that probably isn't going to be relevant for C++ for a
long time. Currently Clang's diagnostics in the case where your header
fails to meet those assumptions are not very good (by that I mean: really
quite poor), but it is something that was recently discussed at the social
and is recognized as an issue; you will primarily have to face this when
actually writing your module map for the first time; after that, it should
just transparently speed up your build.

I've attached a "minimal example" that should illustrate how it works.

And really, what is the difference between "#include <foo.h>" and "import
mymodule.foo"? The real difference is that one can be used in your existing
codebase (in conjunction with a module map) and give you real build
performance wins for your real production C++ code right now (even if you
have to support other compilers), and the other one a) isn't enabled now b)
will probably not be what is actually standardized, and so will never work.
How to organize the "dotted import paths" is a big bikeshed to paint (e.g.
is std.string <string.h> or <string>? is it std.c.io or std.c.stdio?) and
ultimately the standard committee will be the one to decide for the
standard library; in your own module maps for C++, I recommend focusing on
making sure your headers are hygienic.

Right now, it is important to make sure your headers are "hygienic" so that
they down the road, they can be composed into modules with nice "dotted
import paths". Btw, you can get the build time improvements as soon as your
headers are "hygienic".

-- Sean Silva



On Fri, Oct 3, 2014 at 4:23 AM, Stephen Kelly <steveire at gmail.com> wrote:

>
> Hello,
>
> SVN revision 218614 documented that C++ modules works in clang.
>
> Using the llvm apt repo on Ubuntu 14.04, I get this result:
>
> $ clang++-3.6 --version
> Ubuntu clang version 3.6.0-svn218873-1~exp1 (trunk) (based on LLVM 3.6.0)
> Target: x86_64-pc-linux-gnu
> Thread model: posix
>
> $ cat modules.cpp
>
> @import std;
>
> $ clang++-3.6 -std=c++1z -fmodules modules.cpp
> modules.cpp:2:1: error: expected unqualified-id
> @import std;
> ^
> 1 error generated.
>
>
>
> How do I try out clang modules support?
>
> Thanks,
>
> Steve.
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20141003/244e6a0a/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: modulestry.zip
Type: application/zip
Size: 958 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20141003/244e6a0a/attachment.zip>


More information about the cfe-dev mailing list