[PATCH] Private Headers for Modules

Lawrence Crowl crowl at google.com
Mon May 20 17:42:11 PDT 2013


Hi doug.gregor,

This patch adds new private headers to the module map. Private
headers may be included from within the module, but not from outside
the module.

The patch does not provide any symbol export control, only inclusion
control. This capability enables a small step towards limiting
unintended use of a module/library.

Add a new declaration to module maps

   private header "filename";


DOCUMENTATION

In docs/Modules.rst, document the declaration.


MODULE

Modify class Module to track private headers.

Add field PrivateHeaders to list the private headers.

Rename field Headers to NormalHeaders.  Update uses to match.

Print private headers in Module::print.


MODULE MAP

Modify class ModuleMap to keep the role of the header as an enum
rather than a boolean.

Add enum ModuleHeaderRole describing the role of the header, normal,
private, and excluded.

To nested class KnownHeader, change bool Excluded to ModuleHeaderRole
Role.

Several functions that take or return a bool Excluded parameter, now
take a ModuleHeaderRole Role parameter.  Several functions that did
not take a parameter, because they only dealt with normal headers,
now take an additional ModuleHeaderRole Role parameter.

Several that take that take or return Module* will instead take or
return KnownHeader, which provides access to the ModuleHeaderRole.

Add syntax for private headers. This syntax is the same as excluded
headers, but substituting the token "private" instead of "exclude".
Add MMToken::PrivateKeyword.

Change the parameters of parseHeaderDecl from two mutually exclusive
SourceLocations to a the leading token and the source location
of that token.  Modify callers to match.  Some checking need not
happen as a result.  Infer the header role from the leading token.


HEADER SEARCH

Modify struct HeaderFileInfo to keep the role of the header.

Add field ModuleMap::ModuleHeaderRole HeaderRole : 2; constructed
with value ModuleMap::NormalHeader by default.


PREPROCESSOR

Modify In Preprocessor::LookupFile to check for inappropriate use
of private headers.  This requires changing the parameter Module
**SuggestedModule to ModuleMap::KnownHeader *SuggestedModule.
To provide diagnostic line information, It also requires adding a
parameter SourceLocation FilenameLoc.  Update callers to match.


FRONTEND ACTIONS

Make a note that private headers will not be top headers.


SERIALIZATION

Adjust serialization.

To enum SubmoduleRecordTypes add SUBMODULE_PRIVATE_HEADER = 13.

In ASTWriter::WriteSubmodules, add BitCodeAbbrev for
SUBMODULE_PRIVATE_HEADER.  Emit PrivateHeaders names.

In ASTWriter.cpp'HeaderFileInfoTrait::EmitData, write HeaderRole
to flag byte.  This addition uses both remaining bits in the byte.

In ASTReader.cpp'HeaderFileInfoTrait::ReadData: read HeaderRole
from flags; Pass HeaderRole to ModMap.addHeader.

In ASTReader.cpp'ASTReader::ReadSubmoduleBlock: Handle
SUBMODULE_PRIVATE_HEADER like the other headers.


TESTING

Add new module tests for private headers. These are pretty minimal.


http://llvm-reviews.chandlerc.com/D834

Files:
  lib/Serialization/ASTReader.cpp
  lib/Serialization/ASTWriter.cpp
  lib/Frontend/VerifyDiagnosticConsumer.cpp
  lib/Frontend/FrontendActions.cpp
  lib/Basic/Module.cpp
  lib/Lex/PPDirectives.cpp
  lib/Lex/Pragma.cpp
  lib/Lex/HeaderSearch.cpp
  lib/Lex/ModuleMap.cpp
  lib/Lex/PPMacroExpansion.cpp
  test/Modules/private1.cpp
  test/Modules/private2.cpp
  test/Modules/private3.cpp
  test/Modules/Inputs/private1/public.h
  test/Modules/Inputs/private1/private.h
  test/Modules/Inputs/private1/module.map
  test/Modules/Inputs/private2/common.h
  include/clang/Basic/DiagnosticLexKinds.td
  include/clang/Basic/Module.h
  include/clang/Lex/HeaderSearch.h
  include/clang/Lex/ModuleMap.h
  include/clang/Lex/Preprocessor.h
  include/clang/Lex/DirectoryLookup.h
  include/clang/Serialization/ASTBitCodes.h
  docs/Modules.rst
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D834.1.patch
Type: text/x-patch
Size: 39947 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130520/00aba8ca/attachment.bin>


More information about the cfe-commits mailing list