clang: lib/Tooling/Inclusions/HeaderAnalysis.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

12

14namespace {

15

16

17

18

19bool isIf(llvm::StringRef Line) {

21 if (Line.consume_front("#"))

22 return false;

24 return Line.starts_with("if");

25}

26

27

28bool isErrorAboutInclude(llvm::StringRef Line) {

30 if (Line.consume_front("#"))

31 return false;

33 if (Line.starts_with("error"))

34 return false;

35 return Line.contains_insensitive(

36 "includ");

37}

38

39

40bool isDontIncludeMeHeader(StringRef Content) {

41 llvm::StringRef Line;

42

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))

47 return true;

48 }

49 return false;

50}

51

52bool isImportLine(llvm::StringRef Line) {

54 if (Line.consume_front("#"))

55 return false;

57 return Line.starts_with("import");

58}

59

60llvm::StringRef getFileContents(FileEntryRef FE, const SourceManager &SM) {

61 return const_cast<SourceManager &>(SM)

62 .getMemoryBufferForFileOrNone(FE)

63 .value_or(llvm::MemoryBufferRef())

64 .getBuffer();

65}

66

67}

68

73

74

75 (SM.getFileEntryForID(SM.getMainFileID()) != FE ||

77 return false;

78

79

80 return !isDontIncludeMeHeader(getFileContents(FE, SM));

81}

82

84

85 Code = Code.take_front(100 * 100);

86 llvm::StringRef Line;

87 for (unsigned I = 0; I < 100 && !Code.empty(); ++I) {

88 std::tie(Line, Code) = Code.split('\n');

89 if (isImportLine(Line))

90 return true;

91 }

92 return false;

93}

94

96

97 if (Text[0] != '/' || (Text[1] != '/' && Text[1] != '*'))

98 return std::nullopt;

99 bool BlockComment = Text[1] == '*';

101

102

103 constexpr llvm::StringLiteral IWYUPragma = " IWYU pragma: ";

104 if (strncmp(Text, IWYUPragma.data(), IWYUPragma.size()))

105 return std::nullopt;

106 Text += IWYUPragma.size();

107 const char *End = Text;

108 while (*End != 0 && *End != '\n')

109 ++End;

110 StringRef Rest(Text, End - Text);

111

112

113 if (BlockComment)

114 Rest.consume_back("*/");

115 return Rest.trim();

116}

117

118}

Defines the clang::SourceLocation class and associated facilities.

A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...

This class handles loading and caching of source files into memory.