You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

296 lines
7.3 KiB

Simplify CMake per-source license notices Per-source copyright/license notice headers that spell out copyright holder names and years are hard to maintain and often out-of-date or plain wrong. Precise contributor information is already maintained automatically by the version control tool. Ultimately it is the receiver of a file who is responsible for determining its licensing status, and per-source notices are merely a convenience. Therefore it is simpler and more accurate for each source to have a generic notice of the license name and references to more detailed information on copyright holders and full license terms. Our `Copyright.txt` file now contains a list of Contributors whose names appeared source-level copyright notices. It also references version control history for more precise information. Therefore we no longer need to spell out the list of Contributors in each source file notice. Replace CMake per-source copyright/license notice headers with a short description of the license and links to `Copyright.txt` and online information available from "https://cmake.org/licensing". The online URL also handles cases of modules being copied out of our source into other projects, so we can drop our notices about replacing links with full license text. Run the `Utilities/Scripts/filter-notices.bash` script to perform the majority of the replacements mechanically. Manually fix up shebang lines and trailing newlines in a few files. Manually update the notices in a few files that the script does not handle.
9 years ago
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCommandArgumentParserHelper.h"
  4. #include <cstring>
  5. #include <iostream>
  6. #include <sstream>
  7. #include <utility>
  8. #include <cm/memory>
  9. #include <cm/optional>
  10. #include <cmext/string_view>
  11. #include "cmCommandArgumentLexer.h"
  12. #include "cmListFileCache.h"
  13. #include "cmMakefile.h"
  14. #include "cmState.h"
  15. #include "cmStringAlgorithms.h"
  16. #include "cmSystemTools.h"
  17. #include "cmValue.h"
  18. int cmCommandArgument_yyparse(yyscan_t yyscanner);
  19. //
  20. cmCommandArgumentParserHelper::cmCommandArgumentParserHelper()
  21. {
  22. this->FileLine = -1;
  23. this->FileName = nullptr;
  24. this->RemoveEmpty = true;
  25. this->NoEscapeMode = false;
  26. this->ReplaceAtSyntax = false;
  27. }
  28. cmCommandArgumentParserHelper::~cmCommandArgumentParserHelper()
  29. {
  30. this->CleanupParser();
  31. }
  32. void cmCommandArgumentParserHelper::SetLineFile(long line, const char* file)
  33. {
  34. this->FileLine = line;
  35. this->FileName = file;
  36. }
  37. const char* cmCommandArgumentParserHelper::AddString(const std::string& str)
  38. {
  39. if (str.empty()) {
  40. return "";
  41. }
  42. auto stVal = cm::make_unique<char[]>(str.size() + 1);
  43. strcpy(stVal.get(), str.c_str());
  44. this->Variables.push_back(std::move(stVal));
  45. return this->Variables.back().get();
  46. }
  47. const char* cmCommandArgumentParserHelper::ExpandSpecialVariable(
  48. const char* key, const char* var)
  49. {
  50. if (!key) {
  51. return this->ExpandVariable(var);
  52. }
  53. if (!var) {
  54. return "";
  55. }
  56. if (strcmp(key, "ENV") == 0) {
  57. std::string str;
  58. if (cmSystemTools::GetEnv(var, str)) {
  59. if (this->EscapeQuotes) {
  60. return this->AddString(cmEscapeQuotes(str));
  61. }
  62. return this->AddString(str);
  63. }
  64. return "";
  65. }
  66. if (strcmp(key, "CACHE") == 0) {
  67. if (cmValue c =
  68. this->Makefile->GetState()->GetInitializedCacheValue(var)) {
  69. if (this->EscapeQuotes) {
  70. return this->AddString(cmEscapeQuotes(*c));
  71. }
  72. return this->AddString(*c);
  73. }
  74. return "";
  75. }
  76. std::ostringstream e;
  77. e << "Syntax $" << key << "{} is not supported. "
  78. << "Only ${}, $ENV{}, and $CACHE{} are allowed.";
  79. this->SetError(e.str());
  80. return nullptr;
  81. }
  82. const char* cmCommandArgumentParserHelper::ExpandVariable(const char* var)
  83. {
  84. if (!var) {
  85. return nullptr;
  86. }
  87. if (this->FileLine >= 0 && strcmp(var, "CMAKE_CURRENT_LIST_LINE") == 0) {
  88. std::string line;
  89. cmListFileBacktrace bt = this->Makefile->GetBacktrace();
  90. cmListFileContext const& top = bt.Top();
  91. if (top.DeferId) {
  92. line = cmStrCat("DEFERRED:"_s, *top.DeferId);
  93. } else {
  94. line = std::to_string(this->FileLine);
  95. }
  96. return this->AddString(line);
  97. }
  98. cmValue value = this->Makefile->GetDefinition(var);
  99. if (!value) {
  100. this->Makefile->MaybeWarnUninitialized(var, this->FileName);
  101. if (!this->RemoveEmpty) {
  102. return nullptr;
  103. }
  104. }
  105. if (this->EscapeQuotes && value) {
  106. return this->AddString(cmEscapeQuotes(*value));
  107. }
  108. return this->AddString(value);
  109. }
  110. const char* cmCommandArgumentParserHelper::ExpandVariableForAt(const char* var)
  111. {
  112. if (this->ReplaceAtSyntax) {
  113. // try to expand the variable
  114. const char* ret = this->ExpandVariable(var);
  115. // if the return was 0 and we want to replace empty strings
  116. // then return an empty string
  117. if (!ret && this->RemoveEmpty) {
  118. return this->AddString("");
  119. }
  120. // if the ret was not 0, then return it
  121. if (ret) {
  122. return ret;
  123. }
  124. }
  125. // at this point we want to put it back because of one of these cases:
  126. // - this->ReplaceAtSyntax is false
  127. // - this->ReplaceAtSyntax is true, but this->RemoveEmpty is false,
  128. // and the variable was not defined
  129. std::string ref = cmStrCat('@', var, '@');
  130. return this->AddString(ref);
  131. }
  132. const char* cmCommandArgumentParserHelper::CombineUnions(const char* in1,
  133. const char* in2)
  134. {
  135. if (!in1) {
  136. return in2;
  137. }
  138. if (!in2) {
  139. return in1;
  140. }
  141. size_t len = strlen(in1) + strlen(in2) + 1;
  142. auto out = cm::make_unique<char[]>(len);
  143. strcpy(out.get(), in1);
  144. strcat(out.get(), in2);
  145. this->Variables.push_back(std::move(out));
  146. return this->Variables.back().get();
  147. }
  148. void cmCommandArgumentParserHelper::AllocateParserType(
  149. cmCommandArgumentParserHelper::ParserType* pt, const char* str, int len)
  150. {
  151. pt->str = nullptr;
  152. if (len == 0) {
  153. len = static_cast<int>(strlen(str));
  154. }
  155. if (len == 0) {
  156. return;
  157. }
  158. auto out = cm::make_unique<char[]>(len + 1);
  159. memcpy(out.get(), str, len);
  160. out.get()[len] = 0;
  161. pt->str = out.get();
  162. this->Variables.push_back(std::move(out));
  163. }
  164. bool cmCommandArgumentParserHelper::HandleEscapeSymbol(
  165. cmCommandArgumentParserHelper::ParserType* pt, char symbol)
  166. {
  167. switch (symbol) {
  168. case '\\':
  169. case '"':
  170. case ' ':
  171. case '#':
  172. case '(':
  173. case ')':
  174. case '$':
  175. case '@':
  176. case '^':
  177. this->AllocateParserType(pt, &symbol, 1);
  178. break;
  179. case ';':
  180. this->AllocateParserType(pt, "\\;", 2);
  181. break;
  182. case 't':
  183. this->AllocateParserType(pt, "\t", 1);
  184. break;
  185. case 'n':
  186. this->AllocateParserType(pt, "\n", 1);
  187. break;
  188. case 'r':
  189. this->AllocateParserType(pt, "\r", 1);
  190. break;
  191. case '0':
  192. this->AllocateParserType(pt, "\0", 1);
  193. break;
  194. default: {
  195. std::ostringstream e;
  196. e << "Invalid escape sequence \\" << symbol;
  197. this->SetError(e.str());
  198. }
  199. return false;
  200. }
  201. return true;
  202. }
  203. void cmCommandArgument_SetupEscapes(yyscan_t yyscanner, bool noEscapes);
  204. int cmCommandArgumentParserHelper::ParseString(std::string const& str,
  205. int verb)
  206. {
  207. if (str.empty()) {
  208. return 0;
  209. }
  210. this->InputSize = str.size();
  211. this->Verbose = verb;
  212. this->Result.clear();
  213. yyscan_t yyscanner;
  214. cmCommandArgument_yylex_init(&yyscanner);
  215. auto* scanBuf = cmCommandArgument_yy_scan_string(str.c_str(), yyscanner);
  216. cmCommandArgument_yyset_extra(this, yyscanner);
  217. cmCommandArgument_SetupEscapes(yyscanner, this->NoEscapeMode);
  218. int res = cmCommandArgument_yyparse(yyscanner);
  219. cmCommandArgument_yy_delete_buffer(scanBuf, yyscanner);
  220. cmCommandArgument_yylex_destroy(yyscanner);
  221. if (res != 0) {
  222. return 0;
  223. }
  224. this->CleanupParser();
  225. if (this->Verbose) {
  226. std::cerr << "Expanding [" << str << "] produced: [" << this->Result << "]"
  227. << std::endl;
  228. }
  229. return 1;
  230. }
  231. void cmCommandArgumentParserHelper::CleanupParser()
  232. {
  233. this->Variables.clear();
  234. }
  235. void cmCommandArgumentParserHelper::Error(const char* str)
  236. {
  237. auto pos = this->InputBufferPos;
  238. auto const isEof = (this->InputSize < this->InputBufferPos);
  239. if (!isEof) {
  240. pos -= this->LastTokenLength;
  241. }
  242. std::ostringstream ostr;
  243. ostr << str << " (" << pos << ")";
  244. this->SetError(ostr.str());
  245. }
  246. void cmCommandArgumentParserHelper::SetMakefile(const cmMakefile* mf)
  247. {
  248. this->Makefile = mf;
  249. }
  250. void cmCommandArgumentParserHelper::SetResult(const char* value)
  251. {
  252. if (!value) {
  253. this->Result.clear();
  254. return;
  255. }
  256. this->Result = value;
  257. }
  258. void cmCommandArgumentParserHelper::SetError(std::string const& msg)
  259. {
  260. // Keep only the first error.
  261. if (this->ErrorString.empty()) {
  262. this->ErrorString = msg;
  263. }
  264. }
  265. void cmCommandArgumentParserHelper::UpdateInputPosition(int const tokenLength)
  266. {
  267. this->InputBufferPos += tokenLength;
  268. this->LastTokenLength = tokenLength;
  269. }