#include "clang/Tooling/Inclusions/HeaderAnalysis.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Lex/HeaderSearch.h"
namespace clang::tooling {
namespace {
bool isIf(llvm::StringRef Line) {
Line = Line.ltrim();
if (!Line.consume_front("#"))
return false;
Line = Line.ltrim();
return Line.starts_with("if");
}
bool isErrorAboutInclude(llvm::StringRef Line) {
Line = Line.ltrim();
if (!Line.consume_front("#"))
return false;
Line = Line.ltrim();
if (!Line.starts_with("error"))
return false;
return Line.contains_insensitive(
"includ");
}
bool isDontIncludeMeHeader(StringRef Content) {
llvm::StringRef Line;
Content = Content.take_front(100 * 100);
for (unsigned I = 0; I < 100 && !Content.empty(); ++I) {
std::tie(Line, Content) = Content.split('\n');
if (isIf(Line) && isErrorAboutInclude(Content.split('\n').first))
return true;
}
return false;
}
bool isImportLine(llvm::StringRef Line) {
Line = Line.ltrim();
if (!Line.consume_front("#"))
return false;
Line = Line.ltrim();
return Line.starts_with("import");
}
llvm::StringRef getFileContents(FileEntryRef FE, const SourceManager &SM) {
return const_cast<SourceManager &>(SM)
.getMemoryBufferForFileOrNone(FE)
.value_or(llvm::MemoryBufferRef())
.getBuffer();
}
}
bool isSelfContainedHeader(FileEntryRef FE, const SourceManager &SM,
const HeaderSearch &HeaderInfo) {
if (!HeaderInfo.isFileMultipleIncludeGuarded(FE) &&
!HeaderInfo.hasFileBeenImported(FE) &&
(SM.getFileEntryForID(SM.getMainFileID()) != FE ||
!codeContainsImports(getFileContents(FE, SM))))
return false;
return !isDontIncludeMeHeader(getFileContents(FE, SM));
}
bool codeContainsImports(llvm::StringRef Code) {
Code = Code.take_front(100 * 100);
llvm::StringRef Line;
for (unsigned I = 0; I < 100 && !Code.empty(); ++I) {
std::tie(Line, Code) = Code.split('\n');
if (isImportLine(Line))
return true;
}
return false;
}
std::optional<StringRef> parseIWYUPragma(const char *Text) {
if (Text[0] != '/' || (Text[1] != '/' && Text[1] != '*'))
return std::nullopt;
bool BlockComment = Text[1] == '*';
Text += 2;
constexpr llvm::StringLiteral IWYUPragma = " IWYU pragma: ";
if (strncmp(Text, IWYUPragma.data(), IWYUPragma.size()))
return std::nullopt;
Text += IWYUPragma.size();
const char *End = Text;
while (*End != 0 && *End != '\n')
++End;
StringRef Rest(Text, End - Text);
if (BlockComment)
Rest.consume_back("*/");
return Rest.trim();
}
}