InputInfo.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //===--- InputInfo.h - Input Source & Type Information ----------*- 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. #ifndef LLVM_CLANG_LIB_DRIVER_INPUTINFO_H
  9. #define LLVM_CLANG_LIB_DRIVER_INPUTINFO_H
  10. #include "clang/Driver/Action.h"
  11. #include "clang/Driver/Types.h"
  12. #include "llvm/Option/Arg.h"
  13. #include <cassert>
  14. #include <string>
  15. namespace clang {
  16. namespace driver {
  17. /// InputInfo - Wrapper for information about an input source.
  18. class InputInfo {
  19. // FIXME: The distinction between filenames and inputarg here is
  20. // gross; we should probably drop the idea of a "linker
  21. // input". Doing so means tweaking pipelining to still create link
  22. // steps when it sees linker inputs (but not treat them as
  23. // arguments), and making sure that arguments get rendered
  24. // correctly.
  25. enum Class {
  26. Nothing,
  27. Filename,
  28. InputArg,
  29. Pipe
  30. };
  31. union {
  32. const char *Filename;
  33. const llvm::opt::Arg *InputArg;
  34. } Data;
  35. Class Kind;
  36. const Action* Act;
  37. types::ID Type;
  38. const char *BaseInput;
  39. static types::ID GetActionType(const Action *A) {
  40. return A != nullptr ? A->getType() : types::TY_Nothing;
  41. }
  42. public:
  43. InputInfo() : InputInfo(nullptr, nullptr) {}
  44. InputInfo(const Action *A, const char *_BaseInput)
  45. : Kind(Nothing), Act(A), Type(GetActionType(A)), BaseInput(_BaseInput) {}
  46. InputInfo(types::ID _Type, const char *_Filename, const char *_BaseInput)
  47. : Kind(Filename), Act(nullptr), Type(_Type), BaseInput(_BaseInput) {
  48. Data.Filename = _Filename;
  49. }
  50. InputInfo(const Action *A, const char *_Filename, const char *_BaseInput)
  51. : Kind(Filename), Act(A), Type(GetActionType(A)), BaseInput(_BaseInput) {
  52. Data.Filename = _Filename;
  53. }
  54. InputInfo(types::ID _Type, const llvm::opt::Arg *_InputArg,
  55. const char *_BaseInput)
  56. : Kind(InputArg), Act(nullptr), Type(_Type), BaseInput(_BaseInput) {
  57. Data.InputArg = _InputArg;
  58. }
  59. InputInfo(const Action *A, const llvm::opt::Arg *_InputArg,
  60. const char *_BaseInput)
  61. : Kind(InputArg), Act(A), Type(GetActionType(A)), BaseInput(_BaseInput) {
  62. Data.InputArg = _InputArg;
  63. }
  64. bool isNothing() const { return Kind == Nothing; }
  65. bool isFilename() const { return Kind == Filename; }
  66. bool isInputArg() const { return Kind == InputArg; }
  67. types::ID getType() const { return Type; }
  68. const char *getBaseInput() const { return BaseInput; }
  69. /// The action for which this InputInfo was created. May be null.
  70. const Action *getAction() const { return Act; }
  71. void setAction(const Action *A) { Act = A; }
  72. const char *getFilename() const {
  73. assert(isFilename() && "Invalid accessor.");
  74. return Data.Filename;
  75. }
  76. const llvm::opt::Arg &getInputArg() const {
  77. assert(isInputArg() && "Invalid accessor.");
  78. return *Data.InputArg;
  79. }
  80. /// getAsString - Return a string name for this input, for
  81. /// debugging.
  82. std::string getAsString() const {
  83. if (isFilename())
  84. return std::string("\"") + getFilename() + '"';
  85. else if (isInputArg())
  86. return "(input arg)";
  87. else
  88. return "(nothing)";
  89. }
  90. };
  91. } // end namespace driver
  92. } // end namespace clang
  93. #endif