Implemented a stand-alone 'private' pragma. Only friends are allowed to include it.

If no friend is already included when symbols from the file are needed, then iwyu
will just bite the bullet and include the private header.

R=csilvers
DELTA=128  (120 added, 1 deleted, 7 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1983
This commit is contained in:
csilvers+iwyu 2011-05-23 23:07:27 +00:00
parent 34494348e5
commit fbde1d912e
9 changed files with 127 additions and 8 deletions

View File

@ -27,7 +27,6 @@
#include "port.h" // for CHECK_
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/raw_ostream.h"
using std::find;
using std::map;
@ -1192,6 +1191,14 @@ vector<string> IncludePicker::GetCandidateHeadersForFilepathIncludedFrom(
retval.push_back(quoted_includee);
} else {
retval = GetCandidateHeadersForFilepath(included_filepath);
if (retval.size() == 1) {
const string& quoted_header = retval[0];
if (GetVisibility(quoted_header) == IncludePicker::kPrivate) {
VERRS(0) << "Warning: "
<< "No public header found to replace the private header "
<< quoted_header << "\n";
}
}
}
// We'll have called ConvertToQuotedInclude on members of retval,
@ -1229,4 +1236,10 @@ bool IncludePicker::HasMapping(const string& map_from_filepath,
return quoted_to == quoted_from; // indentity mapping, why not?
}
IncludePicker::Visibility IncludePicker::GetVisibility(
const string& quoted_include) const {
return GetOrDefault(
filepath_visibility_map_, quoted_include, kUnusedVisibility);
}
} // namespace include_what_you_use

View File

@ -181,6 +181,10 @@ class IncludePicker {
void MarkVisibility(const string& quoted_include,
IncludePicker::Visibility vis);
// Return the visibility of a given quoted_include if known, else
// kUnusedVisibility.
Visibility GetVisibility(const string& quoted_include) const;
// For the given key, return the vector of values associated with
// that key, or an empty vector if the key does not exist in the
// map, filtering out private files.

View File

@ -213,6 +213,15 @@ void IwyuPreprocessorInfo::ProcessPragmasInFile(SourceLocation file_beginning) {
continue;
}
if (MatchOneToken(tokens, "private", 1, current_loc)) {
const string quoted_include
= ConvertToQuotedInclude(GetFilePath(current_loc));
MutableGlobalIncludePicker()->MarkIncludeAsPrivate(quoted_include);
ERRSYM(GetFileEntry(current_loc)) << "Adding private include: "
<< quoted_include << "\n";
continue;
}
if (MatchOneToken(tokens, "no_include", 2, current_loc)) {
// 2nd token should be an quoted header.
no_include_map_[GetFileEntry(current_loc)].insert(tokens[1]);
@ -229,7 +238,8 @@ void IwyuPreprocessorInfo::ProcessPragmasInFile(SourceLocation file_beginning) {
if (!IsQuotedInclude(regex))
regex = "\"(" + regex + ")\"";
ERRSYM(GetFileEntry(current_loc))
<< GetFilePath(current_loc) << " adding friend regex " << regex << "\n";
<< GetFilePath(current_loc)
<< " adding friend regex " << regex << "\n";
MutableGlobalIncludePicker()->AddFriendRegex(
GetFilePath(current_loc), regex);
continue;

View File

@ -27,12 +27,12 @@
// constructs we look for:
// Full-line constructs:
// a) // IWYU pragma: private, include "foo/bar/baz.h"
// b) // IWYU pragma: begin_exports
// c) // IWYU pragma: end_exports
// d) // IWYU pragma: no_include "foo/bar/baz.h"
// e) // IWYU pragma: friend foo/bar/*
// // IWYU pragma: friend "foo/quotes needed if spaces in path/*"
// // IWYU pragma: friend foo/just_one_file.cc
// b) // IWYU pragma: private
// c) // IWYU pragma: begin_exports
// d) // IWYU pragma: end_exports
// e) // IWYU pragma: no_include "foo/bar/baz.h"
// f) // IWYU pragma: friend <regexp>
// // IWYU pragma: friend "<regexp>" -- needed if spaces in regexp.
// 'Annotation' constructs:
// d) #include "foo/bar/baz.h" // IWYU pragma: export
// e) #include "foo/bar/baz.h" // IWYU pragma: keep

View File

@ -0,0 +1,19 @@
//===--- comment_pragmas-d12.h - test input file for iwyu -----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// This is a private file included directly by comment_pragmas.cc.
#ifndef DEVTOOLS_MAINTENANCE_INCLUDE_WHAT_YOU_USE_TESTS_COMMENT_PRAGMAS_D12_H_
#define DEVTOOLS_MAINTENANCE_INCLUDE_WHAT_YOU_USE_TESTS_COMMENT_PRAGMAS_D12_H_
// IWYU pragma: private
// IWYU pragma: friend tests/comment_pragmas.cc
class CommentPragmasD12 {};
#endif // DEVTOOLS_MAINTENANCE_INCLUDE_WHAT_YOU_USE_TESTS_COMMENT_PRAGMAS_D12_H_

View File

@ -0,0 +1,17 @@
//===--- comment_pragmas-d13.h - test input file for iwyu -----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// This is a private file included directly by comment_pragmas.cc.
#ifndef DEVTOOLS_MAINTENANCE_INCLUDE_WHAT_YOU_USE_TESTS_COMMENT_PRAGMAS_D13_H_
#define DEVTOOLS_MAINTENANCE_INCLUDE_WHAT_YOU_USE_TESTS_COMMENT_PRAGMAS_D13_H_
#include "tests/comment_pragmas-i10.h" // IWYU pragma: export
#endif // DEVTOOLS_MAINTENANCE_INCLUDE_WHAT_YOU_USE_TESTS_COMMENT_PRAGMAS_D13_H_

View File

@ -0,0 +1,19 @@
//===--- comment_pragmas-d14.h - test input file for iwyu ------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// A public header file exporting the symbol CommentPragmasD14.
#ifndef DEVTOOLS_MAINTENANCE_INCLUDE_WHAT_YOU_USE_TESTS_COMMENT_PRAGMAS_D14_H_
#define DEVTOOLS_MAINTENANCE_INCLUDE_WHAT_YOU_USE_TESTS_COMMENT_PRAGMAS_D14_H_
// IWYU pragma: private
// IWYU pragma: friend nobody
class CommentPragmasD14 {};
#endif // DEVTOOLS_MAINTENANCE_INCLUDE_WHAT_YOU_USE_TESTS_COMMENT_PRAGMAS_D14_H_

View File

@ -0,0 +1,19 @@
//===--- comment_pragmas-i10.h - test input file for iwyu ------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// A public header file exporting the symbol CommentPragmasI10.
#ifndef DEVTOOLS_MAINTENANCE_INCLUDE_WHAT_YOU_USE_TESTS_COMMENT_PRAGMAS_I10_H_
#define DEVTOOLS_MAINTENANCE_INCLUDE_WHAT_YOU_USE_TESTS_COMMENT_PRAGMAS_I10_H_
// IWYU pragma: private
// IWYU pragma: friend tests/.*-d13.h
class CommentPragmasI10 {};
#endif // DEVTOOLS_MAINTENANCE_INCLUDE_WHAT_YOU_USE_TESTS_COMMENT_PRAGMAS_I10_H_

View File

@ -62,6 +62,9 @@
#include "tests/comment_pragmas-d1.h"
#include "tests/comment_pragmas-d10.h"
#include "tests/comment_pragmas-d11.h"
#include "tests/comment_pragmas-d12.h"
#include "tests/comment_pragmas-d13.h"
#include "tests/comment_pragmas-d14.h"
#include "tests/comment_pragmas-d2.h"
#include "tests/comment_pragmas-d3.h"
#include "tests/comment_pragmas-d4.h"
@ -120,6 +123,18 @@ CommentPragmasI9 cpi9;
// it.
CommentPragmasD11 cpd11;
// d11.h is a private file with no preferred includer. It has a
// pragma saying it's ok for comment_pragmas.cc to include it.
CommentPragmasD12 cpd12;
// i10.h is included by d13.h. There's a pragma in i10.h declaring
// .*-d13.h friends.
CommentPragmasI10 cpi10;
// d14.h is a private file and we're not a friend.
// IWYU doesn't modify the inclusion.
CommentPragmasD14 cpd14;
/**** IWYU_SUMMARY
tests/comment_pragmas.cc should add these lines:
@ -144,6 +159,9 @@ tests/comment_pragmas.cc should remove these lines:
The full include-list for tests/comment_pragmas.cc:
#include <some_system_header_file> // for CommentPragmasD8, CommentPragmasD9
#include "tests/comment_pragmas-d11.h" // for CommentPragmasD11
#include "tests/comment_pragmas-d12.h" // for CommentPragmasD12
#include "tests/comment_pragmas-d13.h" // for CommentPragmasI10
#include "tests/comment_pragmas-d14.h" // for CommentPragmasD14
#include "tests/comment_pragmas-d5.h"
#include "tests/comment_pragmas-d6.h"
#include "tests/comment_pragmas-i1.h" // for CommentPragmasI2, CommentPragmasI3, CommentPragmasI4