summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZachary Henkel <zachary.henkel@gmail.com>2019-06-13 11:53:55 -0500
committerKim Grasman <kim.grasman@gmail.com>2019-07-12 17:24:00 +0200
commit91c0b55f7f11c2852a0be75f1a598a72d26ab0a7 (patch)
tree9715b74718d691e29dd0498b3069fc3bad89b368
parent4b3ebf24ab1a8cd02100c5db0df56f1d4e250543 (diff)
Don't crash if using decl comes from PCH
If a using-declaration is in a precompiled header, we won't have any file info registered for it. Motivating use case: // main.cpp int main() { A a; } // pch.h struct A {}; using ::A; $ clang++ -x c++-header pch.h -o pch.h.pch $ include-what-you-use.exe -c -include pch.h main.cpp Work around this by skipping using declarations without an associated file, but assert that this only happens if we have an implicit PCH on the command-line. We don't have infrastructure for testing with actual precompiled headers, so no test case. Originally PR #688 by Zachary Henkel, squashed by Kim Gräsman.
-rw-r--r--iwyu.cc15
1 files changed, 14 insertions, 1 deletions
diff --git a/iwyu.cc b/iwyu.cc
index 6262b1d..24ec59a 100644
--- a/iwyu.cc
+++ b/iwyu.cc
@@ -136,6 +136,7 @@
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Lex/Preprocessor.h"
+#include "clang/Lex/PreprocessorOptions.h"
#include "clang/Sema/Sema.h"
namespace clang {
@@ -3708,7 +3709,19 @@ class IwyuAstConsumer
// though, because that will drag in every overload even if we're
// only using one. Instead, we keep track of the using decl and
// mark it as touched when something actually uses it.
- preprocessor_info().FileInfoFor(CurrentFileEntry())->AddUsingDecl(decl);
+ IwyuFileInfo* file_info =
+ preprocessor_info().FileInfoFor(CurrentFileEntry());
+ if (file_info) {
+ file_info->AddUsingDecl(decl);
+ } else {
+ // For using declarations in a PCH, the preprocessor won't have any
+ // location information. As far as we know, that's the only time the
+ // file-info will be null, so assert that we have a PCH on the
+ // command-line.
+ const string& pch_include =
+ compiler()->getInvocation().getPreprocessorOpts().ImplicitPCHInclude;
+ CHECK_(!pch_include.empty());
+ }
if (CanIgnoreCurrentASTNode()) return true;