Commit Graph

59 Commits

Author SHA1 Message Date
Kim Gräsman bd305afe7d Support 'IWYU pragma: export' for forward declarations
Covers both begin_export/end_export blocks and single-line export
pragmas.

Like with 'IWYU pragma: keep' marks the forward decl as automatically
desired to avoid removing manually exported but unused decls.

Add a simple testcase and update documentation.
2023-01-22 20:32:32 +01:00
Kim Gräsman 2dc0645882 Simplify pragma keep handling for forward declarations
Rename ForwardDeclareInKeepRange to ForwardDeclareIsMarkedKeep, take a
NamedDecl and move all location and text wrangling into the function.

This is a layering violation in a sense, because the preprocessor
doesn't know anything about declarations. But the API is much cleaner
that way from the callers' perspective -- they typically just have a
decl and shouldn't be trusted to use the right location info.

No functional change.
2023-01-22 20:32:32 +01:00
Kim Gräsman 849c2a2f55 Format all lines with trailing whitespace
This is the result of:

    git grep -n "\s$" | grep-format

No functional change.
2023-01-07 12:27:16 +01:00
Kim Gräsman e28a2853e7 [clang compat] Adopt OptionalFileEntryRef
Clang 854c10f8d185286d941307e1033eb492e085c203 changed
PPCallbacks::InclusionDirective and Preprocessor::LookupFile to use
clang::OptionalFileEntryRef instead of llvm::Optional<FileEntryRef>.

Update our overrides and calls to match.
2022-12-20 19:26:56 +01:00
Daniel Hannon cf1624a4e2 Add begin_keep and end_keep pragmas, test and docs
this was a proposed issue in #1095 where one could
make a block of keeps as opposed to marking every
keep with a pragma: keep instruction.

added test to verify and updated documentation
where appropriate.
2022-11-02 19:33:10 +01:00
Kim Gräsman 046ca22380 [clang compat] Use FileEntryRef in PPCallbacks::InclusionDirective()
Clang d79ad2f1dbc2db63121620f55d6cfa915f2733ac changed the File parameter from
const FileEntry* to Optional<FileEntryRef>. Parameter is unused in our
implementation, so this is only type system gymnastics.
2022-04-21 20:51:50 +02:00
Alexey Storozhev a2a92b6c6d Delete unused `using` declarations 2021-02-08 07:08:24 +01:00
Kim Grasman 7c2ec8f05b Rename port.h -> iwyu_port.h 2019-12-26 16:12:02 +01:00
Kim Grasman c2d74ac67a Clang r369998: FileSkipped now takes FileEntryRef
Update FileSkipped override to use the new type.

There may be opportunities for us later to store the FileEntryRef to get
at the name as-used-when-opened, but for now just try to get back to a
buildable state.
2019-09-01 16:29:49 +02:00
Kim Grasman 2b596dae66 Simplify handling of defined() operator
Clang r350891 changed the raw lexer so it asserts for IWYU's use
case.

Instead of re-lexing to catch symbols inside defined(), use a
PPCallbacks::Defined override to capture the tokens directly
(presumably, Defined did not exist when the open-coded parser was
added).

This removes a lot of code, including the PPCallbacks for If and Elif,
which are no longer necessary.

No functional change.
2019-01-19 11:13:41 +01:00
Kim Grasman 88af04c321 Update for Clang r332021
PPCallbacks::InclusionDirective grew a new parameter.
2018-05-12 10:27:45 +02:00
Kim Grasman 23253ec2e8 Add pragma to set associated header
Sometimes IWYU's auto-detection of associated headers is insufficient.
Add an IWYU pragma to explicitly set associated header.

This is based on an original patch by Ivan Koster.

I added a test and some documentation.
2017-03-10 21:30:35 +01:00
EugeneZelenko eb7f5d1ef0 Fix Clang-tidy warnings
- readability-redundant-member-init warnings
- modernize nested templates; '> >'
- modernize-use-default warnings
2016-11-16 21:04:24 +01:00
Kim Grasman bb61a50408 Terminology: as-typed -> as-written
Clang uses mostly "as-written" in its APIs. Follow that convention
more consistently.
2016-09-04 17:06:41 +02:00
Kim Grasman dcbc9cc66b More inconsistent parameter names (to be squashed) 2016-08-15 22:31:19 +02:00
Kim Grasman 94949d09a0 Add comments for closing namespace brace
Suggested by clang-tidy, original patch by Eugene Zelenko.

No functional change.
2016-08-15 22:07:27 +02:00
Kim Grasman aa2dd1bb8f Whitespace changes from clang-(tidy|format)
Presumably this makes formatting more consistent.
No functional change.

This is based on an original patch by Eugene Zelenko, thanks!
2016-08-15 22:04:47 +02:00
Volodymyr Sapsai 04f1c92537 Handle internal headers guarded by macro and x-macros (fix issue #109).
I've made a few trade-offs in implementation which I'd like to explain more.

First, code doesn't distinguish between guarded internal headers and
headers with x-macros. They are handled the same way. But in tests both
patterns are tested. It is done not to cover all code paths but to test
include-what-you-use from user's perspective.

Also I check if file defining macro is immediate includer. I decided not
to check if it includes file using the macro transitively until we have
such real-life use cases. Current implementation is strict in order to avoid
unexpected results.

For some cases I am reusing mechanism that keeps files included with the
"IWYU pragma: keep" comment. The downside is that it keeps all lines
including this file which might be not entirely correct for x-macros.
Though x-macros are close to pure textual includes and we cannot reason
about textual includes.

I also didn't include a few test cases because I think they don't
represent real-life use cases. These test cases are:
* when associated header is included by non-associated header;
* when file defining macro and file using macro are both included by the
  third file.
2016-08-14 17:28:46 -07:00
EugeneZelenko d9f88454ca Fix Clang-tidy modernize warnings
In iwyu_preprocessor.h.

NULL/0 -> nullptr
virtual -> override
2016-06-07 22:03:37 +02:00
Kim Grasman e58967a2ec Make header guards consistent
- Remove DEVTOOLS_MAINTENANCE_ from header guards, that was a
  now-unnecessary Googleism

- Fix header guard to match filename in all production code

- Fix header guard to match path in all tests
2016-05-25 22:17:31 +02:00
Volodymyr Sapsai 51fd96f226 Revert "Handle internal headers guarded by macro and x-macros (fix issue #109)."
This reverts commit 5fe99b054b.

It hits assertion
iwyu_include_picker.cc:986: Assertion failed: filepath_visibility_map_[quoted_filepath_pattern] == vis Same file seen with two different visibilities
2016-05-04 21:52:14 -07:00
Volodymyr Sapsai 5fe99b054b Handle internal headers guarded by macro and x-macros (fix issue #109).
I've made a few trade-offs in implementation which I'd like to explain more.

First, code doesn't distinguish between guarded internal headers and
headers with x-macros. They are handled the same way. But in tests both
patterns are tested. It is done not to cover all code paths but to test
include-what-you-use from user's perspective.

Also I check if file defining macro is immediate includer. I decided not
to check if it includes file using the macro transitively until we have
such real-life use cases. Current implementation is strict in order to avoid
unexpected results.

Note, that file using macro should be included with #include, not with
defines LLONG_MIN, LLONG_MAX and #include_next system <limits.h> which
on Mac OS X uses [2] LLONG_MIN and LLONG_MAX in C++11 mode.

For some cases I am reusing mechanism that keeps files included with the
"IWYU pragma: keep" comment. The downside is that it keeps all lines
including this file which might be not entirely correct for x-macros.
Though x-macros are close to pure textual includes and we cannot reason
about textual includes.

I also didn't include a few test cases because I think they don't
represent real-life use cases. These test cases are:
* when associated header is included by non-associated header;
* when file defining macro and file using macro are both included by the
  third file.

[1] http://clang.llvm.org/doxygen/limits_8h_source.html
[2] http://opensource.apple.com/source/Libc/Libc-1082.20.4/include/limits.
2016-05-03 22:52:29 -07:00
Kim Grasman ecf25e0e6e Assume override keyword support. 2015-07-14 22:19:13 +02:00
Volodymyr Sapsai af82b70b6c Update to reflect changes in Clang.
Clang r236404 replaced MacroDirective with MacroDefinition in a few PPCallbacks
methods.
2015-05-06 07:56:27 +00:00
Kim Gräsman ea998345df Remove redundant num_include_directives_.
Found that we already had this information by way of num_includes_seen_.
2014-08-17 08:28:24 +00:00
Kim Gräsman 452df335a0 Fix issue 126: Precompiled-headers support with --prefix_header_includes
IWYU now supports a new --pch_in_code switch which indicates the first
include directive in the translation unit is a precompiled header.
Precompiled headers are treated as prefix headers, even though they
were not specified on the command-line, but they can never be removed.
Finally, they sort first in include lists generated by IWYU, because
most compilers require the PCH to be included before anything else.
2014-04-27 20:35:44 +00:00
Kim Gräsman 45312545a8 Update to reflect changes in Clang.
Clang r196648 - Changed ConditionValue argument to PPCallbacks If and Elif callbacks to be a 3-state enum.
2013-12-07 19:37:19 +00:00
Kim Gräsman 866be38479 Update to reflect changes in Clang.
In Clang r186547 the resulting value of expressions in #if and #elif was added as an argument to PPCallbacks.
2013-07-19 08:38:02 +00:00
Volodymyr Sapsai 62397c669b Update to reflect changes in Clang.
In Clang r181065 MacroArgs argument was added to MacroExpands preprocessor
callback.
2013-05-04 20:53:56 +00:00
Kim Gräsman f64f43f598 Update to reflect changes in Clang.
Clang r175978 changed PPCallbacks to take MacroDirective instead of MacroInfo.
2013-02-25 21:43:57 +00:00
Volodymyr Sapsai 64d55293dd Update to reflect changes in preprocessor callbacks. Patch by Kim Gräsman.
Change was done in Clang r169665, commit message is
"[Preprocessor] Enhance Ifdef/Ifndef/Defined preprocessor callbacks to also pass
a MacroInfo object if the identifier was a macro name."
2012-12-09 14:09:00 +00:00
Volodymyr Sapsai 32dcf20d71 Update to reflect changes in Clang. Now InclusionDirective callback has FilenameRange argument. 2012-09-30 20:07:16 +00:00
Volodymyr Sapsai 1e43b5df1a Instead of include location use included filename location in
GetIncludeNameAsTyped. In case of macro use spelling location (issue #67).
2012-06-12 20:40:02 +00:00
Volodymyr Sapsai bacba2895a Update to reflect changes in Clang (fixes issue #68).
Updated some iterator types (caused errors), fixed preprocessor callbacks and added new enumeration value CK_CopyAndAutoreleaseBlockObject.
2012-05-10 21:10:00 +00:00
csilvers+iwyu 0007a3a5d3 Now that we get the exiting-from file via the FileChanged API,
we don't have to store it ourself anymore.  A minor code
clean-up.

R=dsturtevant
DELTA=16  (4 added, 6 deleted, 6 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=3485
2011-10-17 20:33:19 +00:00
csilvers+iwyu c8131fdc86 Fix up a few API changes.
R=nlewycky,csilvers,chrsmith


Revision created by MOE tool push_codebase.
MOE_MIGRATION=3484
2011-10-17 20:32:51 +00:00
csilvers+iwyu 8f439f6164 When we detect an include-mapping cycle, print the cycle.
This will help a lot in debugging.

We don't have great testing for this code, but I tested it
manually on a file that has an iwyu pragma that maps an
include to itself (creating a cycle of size 1).

R=dsturtevant
DELTA=13  (10 added, 0 deleted, 3 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=3462
2011-10-17 20:32:27 +00:00
csilvers 0353ab58d2 FileChanged takes an extra argument now. Unbreaks the build. 2011-10-12 20:42:39 +00:00
csilvers+iwyu ac177329d5 Fix a bug that would come up when using -I -- when looking for
associated header files, we would use the filename as it
exists on the filesystem, not the one that is already used in
the code (due to -I).  As a result, we could suggest a user
add an #include of an associated .h file, even if they were
already #including it (but typed differently due to -I).

Patch is from vasp...@gmail.com, at
http://code.google.com/p/include-what-you-use/issues/detail?id=5#c25

R=dsturtevant,chandlerc
DELTA=77  (71 added, 0 deleted, 6 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=3430
2011-10-07 21:51:59 +00:00
csilvers+iwyu 8ddb7ca81d New pragma: no_forward_declare <qualified symbol name>. When IWYU sees this,
it should treat all forward declarable uses of the given symbol in the
file as full info uses.

R=csilvers
DELTA=104  (92 added, 0 deleted, 12 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=3113
2011-08-26 11:40:39 +00:00
csilvers+iwyu 49c6d13b21 Make no-op change as Clang changed the PPCallbacks interface.
R=chandlerc,csilvers


Revision created by MOE tool push_codebase.
MOE_MIGRATION=3064
2011-08-19 22:35:51 +00:00
csilvers+iwyu 3392c4d1e9 Improve the heuristic for identifying associated .h files.
Here's the new (additional) rule:

If there's is a .h in the first include of a .cc its basename
is the same as the .cc except for the extension then assume
it's the header implementing that file.

I had to modify the test framework a bit to capture test files
in subdirectories.

R=nilton
DELTA=91  (77 added, 0 deleted, 14 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=2483
2011-07-02 00:06:22 +00:00
csilvers+iwyu 1a614ece2e Some changes based on csilvers' suggestions.
R=csilvers
DELTA=30  (17 added, 10 deleted, 3 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1985
2011-05-23 23:08:27 +00:00
csilvers+iwyu 4ec5178af6 Use a CommentHandler for most pragma parsing. A side effect is that the
identification of what is a pragma is now correct, because it must
start the comment.

R=wan,csilvers
DELTA=189  (58 added, 32 deleted, 99 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1984
2011-05-23 23:07:56 +00:00
csilvers+iwyu fbde1d912e 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
2011-05-23 23:07:27 +00:00
csilvers+iwyu 256da9418d Include-what-you-use fixit -- run iwyu on itself to fix up includes (part 2).
R=csilvers
DELTA=51  (3 added, 47 deleted, 1 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1736
2011-05-04 18:32:59 +00:00
csilvers+iwyu 5ca17d139e Include-what-you-use fixit -- fix #includes on iwyu itself.
R=csilvers
DELTA=123  (22 added, 5 deleted, 96 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1732
2011-05-04 18:30:53 +00:00
csilvers+iwyu 3d55404056 Include-what-you-use fixes by running it on itself.
R=dsturtevant,csilvers
	DELTA=215  (135 added, 45 deleted, 35 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1731
2011-05-04 18:29:59 +00:00
csilvers+iwyu ca253cf0e2 New pragma "friend". Usage:
// IWYU pragma: friend <glob>
Filenames matching the glob are allowed to include the file, which has presumably
been declared private.

R=wan,csilvers
DELTA=123  (106 added, 7 deleted, 10 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1719
2011-05-04 18:17:55 +00:00
csilvers+iwyu 926fa38b98 Allow include-what-you-use to run in
--transitive_includes_only mode (controlled by a flag).  In
this mode, we will throw out suggestions that file a #include
file b if file b is not visible in file a's transitive
includes; that is, all we do is move indirect includes to
direct includes, we never add a 'totally new' include.

While it's certainly possible for a 'totally new' include
suggestion to be correct, in our experience it's much more
commonly an iwyu error, usually because iwyu associates a use
with the wrong file.  We control this by a flag so the user
can decide themselves whether they care more about false
positives or false negatives.

R=dsturtevant
DELTA=132  (123 added, 2 deleted, 7 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1592
2011-04-26 23:13:25 +00:00