FormatStringParsing.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #ifndef LLVM_CLANG_LIB_ANALYSIS_FORMATSTRINGPARSING_H
  2. #define LLVM_CLANG_LIB_ANALYSIS_FORMATSTRINGPARSING_H
  3. #include "clang/AST/ASTContext.h"
  4. #include "clang/AST/Type.h"
  5. #include "clang/Analysis/Analyses/FormatString.h"
  6. namespace clang {
  7. class LangOptions;
  8. template <typename T>
  9. class UpdateOnReturn {
  10. T &ValueToUpdate;
  11. const T &ValueToCopy;
  12. public:
  13. UpdateOnReturn(T &valueToUpdate, const T &valueToCopy)
  14. : ValueToUpdate(valueToUpdate), ValueToCopy(valueToCopy) {}
  15. ~UpdateOnReturn() {
  16. ValueToUpdate = ValueToCopy;
  17. }
  18. };
  19. namespace analyze_format_string {
  20. OptionalAmount ParseAmount(const char *&Beg, const char *E);
  21. OptionalAmount ParseNonPositionAmount(const char *&Beg, const char *E,
  22. unsigned &argIndex);
  23. OptionalAmount ParsePositionAmount(FormatStringHandler &H,
  24. const char *Start, const char *&Beg,
  25. const char *E, PositionContext p);
  26. bool ParseFieldWidth(FormatStringHandler &H,
  27. FormatSpecifier &CS,
  28. const char *Start, const char *&Beg, const char *E,
  29. unsigned *argIndex);
  30. bool ParseArgPosition(FormatStringHandler &H,
  31. FormatSpecifier &CS, const char *Start,
  32. const char *&Beg, const char *E);
  33. /// Returns true if a LengthModifier was parsed and installed in the
  34. /// FormatSpecifier& argument, and false otherwise.
  35. bool ParseLengthModifier(FormatSpecifier &FS, const char *&Beg, const char *E,
  36. const LangOptions &LO, bool IsScanf = false);
  37. /// Returns true if the invalid specifier in \p SpecifierBegin is a UTF-8
  38. /// string; check that it won't go further than \p FmtStrEnd and write
  39. /// up the total size in \p Len.
  40. bool ParseUTF8InvalidSpecifier(const char *SpecifierBegin,
  41. const char *FmtStrEnd, unsigned &Len);
  42. template <typename T> class SpecifierResult {
  43. T FS;
  44. const char *Start;
  45. bool Stop;
  46. public:
  47. SpecifierResult(bool stop = false)
  48. : Start(nullptr), Stop(stop) {}
  49. SpecifierResult(const char *start,
  50. const T &fs)
  51. : FS(fs), Start(start), Stop(false) {}
  52. const char *getStart() const { return Start; }
  53. bool shouldStop() const { return Stop; }
  54. bool hasValue() const { return Start != nullptr; }
  55. const T &getValue() const {
  56. assert(hasValue());
  57. return FS;
  58. }
  59. const T &getValue() { return FS; }
  60. };
  61. } // end analyze_format_string namespace
  62. } // end clang namespace
  63. #endif