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.
This commit is contained in:
parent
f7c5a795ab
commit
2dc0645882
9
iwyu.cc
9
iwyu.cc
|
@ -3987,13 +3987,8 @@ class IwyuAstConsumer
|
|||
definitely_keep_fwd_decl = true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
SourceLocation decl_end_location = decl->getSourceRange().getEnd();
|
||||
if (LineHasText(decl_end_location, "// IWYU pragma: keep") ||
|
||||
LineHasText(decl_end_location, "/* IWYU pragma: keep") ||
|
||||
preprocessor_info().ForwardDeclareInKeepRange(decl_end_location)) {
|
||||
definitely_keep_fwd_decl = true;
|
||||
}
|
||||
} else if (preprocessor_info().ForwardDeclareIsMarkedKeep(decl)) {
|
||||
definitely_keep_fwd_decl = true;
|
||||
}
|
||||
|
||||
preprocessor_info().FileInfoFor(CurrentFileEntry())->AddForwardDeclare(
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
#include "iwyu_string_util.h"
|
||||
#include "iwyu_verrs.h"
|
||||
#include "llvm/Support/raw_ostream.h"
|
||||
#include "clang/AST/Decl.h"
|
||||
#include "clang/Basic/IdentifierTable.h"
|
||||
#include "clang/Lex/MacroInfo.h"
|
||||
|
||||
|
@ -37,6 +38,7 @@ using clang::FileID;
|
|||
using clang::MacroDefinition;
|
||||
using clang::MacroDirective;
|
||||
using clang::MacroInfo;
|
||||
using clang::NamedDecl;
|
||||
using clang::Preprocessor;
|
||||
using clang::SourceLocation;
|
||||
using clang::SourceRange;
|
||||
|
@ -1123,7 +1125,12 @@ bool IwyuPreprocessorInfo::ForwardDeclareIsInhibited(
|
|||
ContainsKey(*inhibited_forward_declares, normalized_symbol_name);
|
||||
}
|
||||
|
||||
bool IwyuPreprocessorInfo::ForwardDeclareInKeepRange(SourceLocation loc) const {
|
||||
bool IwyuPreprocessorInfo::ForwardDeclareIsMarkedKeep(
|
||||
const NamedDecl* decl) const {
|
||||
// Use end-location so that any trailing comments match only on the last line.
|
||||
SourceLocation loc = decl->getEndLoc();
|
||||
|
||||
// Is the decl part of a begin_keep/end_keep block?
|
||||
const FileEntry* file = GetFileEntry(loc);
|
||||
auto keep_ranges = keep_location_ranges_.equal_range(file);
|
||||
for (auto it = keep_ranges.first; it != keep_ranges.second; ++it) {
|
||||
|
@ -1131,7 +1138,8 @@ bool IwyuPreprocessorInfo::ForwardDeclareInKeepRange(SourceLocation loc) const {
|
|||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
// Is the declaration itself marked with trailing comment?
|
||||
return (LineHasText(loc, "// IWYU pragma: keep") ||
|
||||
LineHasText(loc, "/* IWYU pragma: keep"));
|
||||
}
|
||||
|
||||
} // namespace include_what_you_use
|
||||
|
|
|
@ -79,6 +79,7 @@
|
|||
namespace clang {
|
||||
class FileEntry;
|
||||
class MacroInfo;
|
||||
class NamedDecl;
|
||||
} // namespace clang
|
||||
|
||||
namespace include_what_you_use {
|
||||
|
@ -168,9 +169,8 @@ class IwyuPreprocessorInfo : public clang::PPCallbacks,
|
|||
bool ForwardDeclareIsInhibited(
|
||||
const clang::FileEntry* file, const string& qualified_symbol_name) const;
|
||||
|
||||
// Return true if the fwd decl is in the range of a begin_keep -> end_keep
|
||||
// block.
|
||||
bool ForwardDeclareInKeepRange(clang::SourceLocation loc) const;
|
||||
// Return true if the fwd decl is marked with "IWYU pragma: keep".
|
||||
bool ForwardDeclareIsMarkedKeep(const clang::NamedDecl* decl) const;
|
||||
|
||||
protected:
|
||||
// Preprocessor event handlers called by Clang.
|
||||
|
|
Loading…
Reference in New Issue