19bool isIf(llvm::StringRef Line) {
21 if (!Line.consume_front(
"#"))
24 return Line.startswith(
"if");
28bool isErrorAboutInclude(llvm::StringRef Line) {
30 if (!Line.consume_front(
"#"))
33 if (!Line.startswith(
"error"))
35 return Line.contains_insensitive(
40bool isDontIncludeMeHeader(StringRef Content) {
43 Content = Content.take_front(100 * 100);
44 for (
unsigned I = 0; I < 100 && !Content.empty(); ++I) {
45 std::tie(Line, Content) = Content.split(
'\n');
46 if (isIf(Line) && isErrorAboutInclude(Content.split(
'\n').first))
52bool isImportLine(llvm::StringRef Line) {
54 if (!Line.consume_front(
"#"))
57 return Line.startswith(
"import");
60llvm::StringRef getFileContents(
const FileEntry *FE,
const SourceManager &
SM) {
61 return const_cast<SourceManager &
>(
SM)
62 .getMemoryBufferForFileOrNone(FE)
63 .value_or(llvm::MemoryBufferRef())
76 (
SM.getFileEntryForID(
SM.getMainFileID()) != FE ||
81 return !isDontIncludeMeHeader(getFileContents(FE,
SM));
86 Code = Code.take_front(100 * 100);
88 for (
unsigned I = 0; I < 100 && !Code.empty(); ++I) {
89 std::tie(Line, Code) = Code.split(
'\n');
90 if (isImportLine(Line))
98 if (
Text[0] !=
'/' || (
Text[1] !=
'/' &&
Text[1] !=
'*'))
100 bool BlockComment =
Text[1] ==
'*';
104 constexpr llvm::StringLiteral IWYUPragma =
" IWYU pragma: ";
105 if (strncmp(
Text, IWYUPragma.data(), IWYUPragma.size()))
107 Text += IWYUPragma.size();
108 const char *End =
Text;
109 while (*End != 0 && *End !=
'\n')
115 Rest.consume_back(
"*/");
Defines the clang::SourceLocation class and associated facilities.
Cached information about one file (either on disk or in the virtual file system).
This class handles loading and caching of source files into memory.