FormatStringParsing.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //===----- FormatStringParsing.h - Format String Parsing --------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This provides some shared functions between printf and scanf format string
  10. // parsing code.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_LIB_ANALYSIS_FORMATSTRINGPARSING_H
  14. #define LLVM_CLANG_LIB_ANALYSIS_FORMATSTRINGPARSING_H
  15. #include "clang/AST/ASTContext.h"
  16. #include "clang/AST/Type.h"
  17. #include "clang/AST/FormatString.h"
  18. namespace clang {
  19. class LangOptions;
  20. template <typename T>
  21. class UpdateOnReturn {
  22. T &ValueToUpdate;
  23. const T &ValueToCopy;
  24. public:
  25. UpdateOnReturn(T &valueToUpdate, const T &valueToCopy)
  26. : ValueToUpdate(valueToUpdate), ValueToCopy(valueToCopy) {}
  27. ~UpdateOnReturn() {
  28. ValueToUpdate = ValueToCopy;
  29. }
  30. };
  31. namespace analyze_format_string {
  32. OptionalAmount ParseAmount(const char *&Beg, const char *E);
  33. OptionalAmount ParseNonPositionAmount(const char *&Beg, const char *E,
  34. unsigned &argIndex);
  35. OptionalAmount ParsePositionAmount(FormatStringHandler &H,
  36. const char *Start, const char *&Beg,
  37. const char *E, PositionContext p);
  38. bool ParseFieldWidth(FormatStringHandler &H,
  39. FormatSpecifier &CS,
  40. const char *Start, const char *&Beg, const char *E,
  41. unsigned *argIndex);
  42. bool ParseArgPosition(FormatStringHandler &H,
  43. FormatSpecifier &CS, const char *Start,
  44. const char *&Beg, const char *E);
  45. bool ParseVectorModifier(FormatStringHandler &H,
  46. FormatSpecifier &FS, const char *&Beg, const char *E,
  47. const LangOptions &LO);
  48. /// Returns true if a LengthModifier was parsed and installed in the
  49. /// FormatSpecifier& argument, and false otherwise.
  50. bool ParseLengthModifier(FormatSpecifier &FS, const char *&Beg, const char *E,
  51. const LangOptions &LO, bool IsScanf = false);
  52. /// Returns true if the invalid specifier in \p SpecifierBegin is a UTF-8
  53. /// string; check that it won't go further than \p FmtStrEnd and write
  54. /// up the total size in \p Len.
  55. bool ParseUTF8InvalidSpecifier(const char *SpecifierBegin,
  56. const char *FmtStrEnd, unsigned &Len);
  57. template <typename T> class SpecifierResult {
  58. T FS;
  59. const char *Start;
  60. bool Stop;
  61. public:
  62. SpecifierResult(bool stop = false)
  63. : Start(nullptr), Stop(stop) {}
  64. SpecifierResult(const char *start,
  65. const T &fs)
  66. : FS(fs), Start(start), Stop(false) {}
  67. const char *getStart() const { return Start; }
  68. bool shouldStop() const { return Stop; }
  69. bool hasValue() const { return Start != nullptr; }
  70. const T &getValue() const {
  71. assert(hasValue());
  72. return FS;
  73. }
  74. const T &getValue() { return FS; }
  75. };
  76. } // end analyze_format_string namespace
  77. } // end clang namespace
  78. #endif