diff options
author | mvel <mvel@yandex-team.ru> | 2022-02-10 16:45:41 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:41 +0300 |
commit | 43f5a35593ebc9f6bcea619bb170394ea7ae468e (patch) | |
tree | e98df59de24d2ef7c77baed9f41e4875a2fef972 /tools | |
parent | bd30392c4cc92487950adc375c07adf52da1d592 (diff) | |
download | ydb-43f5a35593ebc9f6bcea619bb170394ea7ae468e.tar.gz |
Restoring authorship annotation for <mvel@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'tools')
-rw-r--r-- | tools/archiver/main.cpp | 280 | ||||
-rw-r--r-- | tools/archiver/tests/directory/file1 | 2 | ||||
-rw-r--r-- | tools/archiver/tests/directory/file2 | 2 | ||||
-rw-r--r-- | tools/archiver/tests/test.py | 72 | ||||
-rw-r--r-- | tools/archiver/tests/ya.make | 6 | ||||
-rw-r--r-- | tools/archiver/ya.make | 8 | ||||
-rw-r--r-- | tools/enum_parser/enum_parser/main.cpp | 564 | ||||
-rw-r--r-- | tools/enum_parser/enum_parser/ya.make | 10 | ||||
-rw-r--r-- | tools/enum_parser/ya.make | 8 | ||||
-rw-r--r-- | tools/ya.make | 10 |
10 files changed, 481 insertions, 481 deletions
diff --git a/tools/archiver/main.cpp b/tools/archiver/main.cpp index daf3b5cf28..6cda54c1ea 100644 --- a/tools/archiver/main.cpp +++ b/tools/archiver/main.cpp @@ -14,7 +14,7 @@ #include <util/string/escape.h> #include <util/string/hex.h> #include <util/string/subst.h> -#include <util/system/filemap.h> +#include <util/system/filemap.h> #include <cstring> @@ -54,7 +54,7 @@ namespace { class THexOutput: public IOutputStream { public: inline THexOutput(IOutputStream* slave) - : Slave_(slave) + : Slave_(slave) { } @@ -62,13 +62,13 @@ namespace { } inline IOutputStream* Slave() const noexcept { - return Slave_; + return Slave_; } private: void DoFinish() override { - Slave_->Write('\n'); - Slave_->Flush(); + Slave_->Write('\n'); + Slave_->Flush(); } void DoWrite(const void* data, size_t len) override { @@ -79,14 +79,14 @@ namespace { char buf[12]; char* tmp = buf; - if (Count_ % Columns == 0) { + if (Count_ % Columns == 0) { *tmp++ = ' '; *tmp++ = ' '; *tmp++ = ' '; *tmp++ = ' '; } - if (Count_ && Count_ % Columns != 0) { + if (Count_ && Count_ % Columns != 0) { *tmp++ = ','; *tmp++ = ' '; } @@ -95,41 +95,41 @@ namespace { *tmp++ = 'x'; tmp = HexEncode(&c, 1, tmp); - if ((Count_ % Columns) == (Columns - 1)) { + if ((Count_ % Columns) == (Columns - 1)) { *tmp++ = ','; *tmp++ = '\n'; } - Slave_->Write(buf, tmp - buf); + Slave_->Write(buf, tmp - buf); --len; ++b; - ++Count_; + ++Count_; } } private: // width in source chars - static const size_t Columns = 10; - ui64 Count_ = 0; + static const size_t Columns = 10; + ui64 Count_ = 0; IOutputStream* Slave_ = nullptr; }; struct TYasmOutput: public IOutputStream { inline TYasmOutput(IOutputStream* out, const TString& base) - : Out_(out) - , Base_(base) + : Out_(out) + , Base_(base) { - *Out_ << "global " << Base_ << "\n"; - *Out_ << "global " << Base_ << "Size\n\nSECTION .rodata\n\n"; - *Out_ << Base_ << ":\n"; + *Out_ << "global " << Base_ << "\n"; + *Out_ << "global " << Base_ << "Size\n\nSECTION .rodata\n\n"; + *Out_ << Base_ << ":\n"; } ~TYasmOutput() override { } void DoFinish() override { - *Out_ << Base_ << "Size:\ndd " << Count_ << '\n'; + *Out_ << Base_ << "Size:\ndd " << Count_ << '\n'; *Out_ << "%ifidn __OUTPUT_FORMAT__,elf64\n"; *Out_ << "size " << Base_ << " " << Count_ << "\n"; @@ -138,20 +138,20 @@ namespace { } void DoWrite(const void* data, size_t len) override { - Count_ += len; + Count_ += len; const unsigned char* p = (const unsigned char*)data; while (len) { const size_t step = Min<size_t>(len, 100); - *Out_ << "db " << (int)*p++; + *Out_ << "db " << (int)*p++; for (size_t i = 1; i < step; ++i) { - *Out_ << ',' << (int)*p++; + *Out_ << ',' << (int)*p++; } - *Out_ << '\n'; + *Out_ << '\n'; len -= step; } @@ -159,7 +159,7 @@ namespace { IOutputStream* Out_ = nullptr; const TString Base_; - ui64 Count_ = 0; + ui64 Count_ = 0; }; struct TCOutput: public THexOutput { @@ -331,9 +331,9 @@ static bool Quiet = false; static inline void Append(IOutputStream& w, const TString& fname, const TString& rname) { TMappedFileInput in(fname); - if (!Quiet) { + if (!Quiet) { Cerr << "--> " << rname << Endl; - } + } TransferData((IInputStream*)&in, &w); } @@ -343,9 +343,9 @@ static inline void Append(TDuplicatesMap& w, const TString& fname, const TString } static inline void Append(TDeduplicationArchiveWriter& w, const TString& fname, const TString& rname) { - if (!Quiet) { + if (!Quiet) { Cerr << "--> " << rname << Endl; - } + } if (const TString* rootRecordName = w.DuplicatesMap.Synonyms.FindPtr(rname)) { w.Writer.AddSynonym(*rootRecordName, rname); @@ -357,12 +357,12 @@ static inline void Append(TDeduplicationArchiveWriter& w, const TString& fname, namespace { struct TRec { - bool Recursive = false; + bool Recursive = false; TString Key; TString Path; TString Prefix; - TRec() = default; + TRec() = default; inline void Fix() { ::Fix(Path); @@ -422,29 +422,29 @@ static TString CutFirstSlash(const TString& fileName) { } } -struct TMappingReader { - TMemoryMap Map; - TBlob Blob; - TArchiveReader Reader; - +struct TMappingReader { + TMemoryMap Map; + TBlob Blob; + TArchiveReader Reader; + TMappingReader(const TString& archive) - : Map(archive) - , Blob(TBlob::FromMemoryMapSingleThreaded(Map, 0, Map.Length())) - , Reader(Blob) - { - } -}; - + : Map(archive) + , Blob(TBlob::FromMemoryMapSingleThreaded(Map, 0, Map.Length())) + , Reader(Blob) + { + } +}; + static void UnpackArchive(const TString& archive, const TFsPath& dir = TFsPath()) { - TMappingReader mappingReader(archive); - const TArchiveReader& reader = mappingReader.Reader; + TMappingReader mappingReader(archive); + const TArchiveReader& reader = mappingReader.Reader; const size_t count = reader.Count(); for (size_t i = 0; i < count; ++i) { const TString key = reader.KeyByIndex(i); const TString fileName = CutFirstSlash(key); - if (!Quiet) { + if (!Quiet) { Cerr << archive << " --> " << fileName << Endl; - } + } const TFsPath path(dir / fileName); path.Parent().MkDirs(); TAutoPtr<IInputStream> in = reader.ObjectByKey(key); @@ -455,8 +455,8 @@ static void UnpackArchive(const TString& archive, const TFsPath& dir = TFsPath() } static void ListArchive(const TString& archive, bool cutSlash) { - TMappingReader mappingReader(archive); - const TArchiveReader& reader = mappingReader.Reader; + TMappingReader mappingReader(archive); + const TArchiveReader& reader = mappingReader.Reader; const size_t count = reader.Count(); for (size_t i = 0; i < count; ++i) { const TString key = reader.KeyByIndex(i); @@ -469,8 +469,8 @@ static void ListArchive(const TString& archive, bool cutSlash) { } static void ListArchiveMd5(const TString& archive, bool cutSlash) { - TMappingReader mappingReader(archive); - const TArchiveReader& reader = mappingReader.Reader; + TMappingReader mappingReader(archive); + const TArchiveReader& reader = mappingReader.Reader; const size_t count = reader.Count(); for (size_t i = 0; i < count; ++i) { const TString key = reader.KeyByIndex(i); @@ -484,125 +484,125 @@ static void ListArchiveMd5(const TString& archive, bool cutSlash) { } int main(int argc, char** argv) { - NLastGetopt::TOpts opts; - opts.AddHelpOption('?'); - opts.SetTitle( - "Archiver\n" + NLastGetopt::TOpts opts; + opts.AddHelpOption('?'); + opts.SetTitle( + "Archiver\n" "Docs: https://wiki.yandex-team.ru/Development/Poisk/arcadia/tools/archiver" - ); - - bool hexdump = false; - opts.AddLongOption('x', "hexdump", "Produce hexdump") - .NoArgument() - .Optional() - .StoreValue(&hexdump, true); - - size_t stride = 0; - opts.AddLongOption('s', "segments", "Produce segmented C strings array of given size") - .RequiredArgument("<size>") - .Optional() - .DefaultValue("0") - .StoreResult(&stride); - - bool cat = false; - opts.AddLongOption('c', "cat", "Do not store keys (file names), just cat uncompressed files") - .NoArgument() - .Optional() - .StoreValue(&cat, true); - - bool doNotZip = false; - opts.AddLongOption('p', "plain", "Do not use compression") - .NoArgument() - .Optional() - .StoreValue(&doNotZip, true); - + ); + + bool hexdump = false; + opts.AddLongOption('x', "hexdump", "Produce hexdump") + .NoArgument() + .Optional() + .StoreValue(&hexdump, true); + + size_t stride = 0; + opts.AddLongOption('s', "segments", "Produce segmented C strings array of given size") + .RequiredArgument("<size>") + .Optional() + .DefaultValue("0") + .StoreResult(&stride); + + bool cat = false; + opts.AddLongOption('c', "cat", "Do not store keys (file names), just cat uncompressed files") + .NoArgument() + .Optional() + .StoreValue(&cat, true); + + bool doNotZip = false; + opts.AddLongOption('p', "plain", "Do not use compression") + .NoArgument() + .Optional() + .StoreValue(&doNotZip, true); + bool deduplicate = false; opts.AddLongOption("deduplicate", "Turn on file-wise deduplication") .NoArgument() .Optional() .StoreValue(&deduplicate, true); - bool unpack = false; - opts.AddLongOption('u', "unpack", "Unpack archive into current directory") - .NoArgument() - .Optional() - .StoreValue(&unpack, true); - - bool list = false; - opts.AddLongOption('l', "list", "List files in archive") - .NoArgument() - .Optional() - .StoreValue(&list, true); - + bool unpack = false; + opts.AddLongOption('u', "unpack", "Unpack archive into current directory") + .NoArgument() + .Optional() + .StoreValue(&unpack, true); + + bool list = false; + opts.AddLongOption('l', "list", "List files in archive") + .NoArgument() + .Optional() + .StoreValue(&list, true); + bool cutSlash = true; opts.AddLongOption("as-is", "somewhy slash is cutted by default in list; with this option key will be shown as-is") .NoArgument() .Optional() .StoreValue(&cutSlash, false); - bool listMd5 = false; - opts.AddLongOption('m', "md5", "List files in archive with MD5 sums") - .NoArgument() - .Optional() - .StoreValue(&listMd5, true); - - bool recursive = false; - opts.AddLongOption('r', "recursive", "Read all files under each directory, recursively") - .NoArgument() - .Optional() - .StoreValue(&recursive, true); - - Quiet = false; - opts.AddLongOption('q', "quiet", "Do not output progress to stderr") - .NoArgument() - .Optional() - .StoreValue(&Quiet, true); - + bool listMd5 = false; + opts.AddLongOption('m', "md5", "List files in archive with MD5 sums") + .NoArgument() + .Optional() + .StoreValue(&listMd5, true); + + bool recursive = false; + opts.AddLongOption('r', "recursive", "Read all files under each directory, recursively") + .NoArgument() + .Optional() + .StoreValue(&recursive, true); + + Quiet = false; + opts.AddLongOption('q', "quiet", "Do not output progress to stderr") + .NoArgument() + .Optional() + .StoreValue(&Quiet, true); + TString prepend; - opts.AddLongOption('z', "prepend", "Prepend string to output") - .RequiredArgument("<prefix>") - .StoreResult(&prepend); - + opts.AddLongOption('z', "prepend", "Prepend string to output") + .RequiredArgument("<prefix>") + .StoreResult(&prepend); + TString append; - opts.AddLongOption('a', "append", "Append string to output") - .RequiredArgument("<suffix>") - .StoreResult(&append); - + opts.AddLongOption('a', "append", "Append string to output") + .RequiredArgument("<suffix>") + .StoreResult(&append); + TString outputf; - opts.AddLongOption('o', "output", "Output to file instead stdout") - .RequiredArgument("<file>") - .StoreResult(&outputf); - + opts.AddLongOption('o', "output", "Output to file instead stdout") + .RequiredArgument("<file>") + .StoreResult(&outputf); + TString unpackDir; - opts.AddLongOption('d', "unpackdir", "Unpack destination directory") - .RequiredArgument("<dir>") - .DefaultValue(".") - .StoreResult(&unpackDir); - + opts.AddLongOption('d', "unpackdir", "Unpack destination directory") + .RequiredArgument("<dir>") + .DefaultValue(".") + .StoreResult(&unpackDir); + TString yasmBase; - opts.AddLongOption('A', "yasm", "Output dump is yasm format") - .RequiredArgument("<base>") - .StoreResult(&yasmBase); - + opts.AddLongOption('A', "yasm", "Output dump is yasm format") + .RequiredArgument("<base>") + .StoreResult(&yasmBase); + TString cppBase; - opts.AddLongOption('C', "cpp", "Output dump is C/C++ format") - .RequiredArgument("<base>") - .StoreResult(&cppBase); - + opts.AddLongOption('C', "cpp", "Output dump is C/C++ format") + .RequiredArgument("<base>") + .StoreResult(&cppBase); + TString forceKeys; opts.AddLongOption('k', "keys", "Set explicit list of keys for elements") .RequiredArgument("<keys>") .StoreResult(&forceKeys); - opts.SetFreeArgDefaultTitle("<file>"); - opts.SetFreeArgsMin(1); - NLastGetopt::TOptsParseResult optsRes(&opts, argc, argv); - + opts.SetFreeArgDefaultTitle("<file>"); + opts.SetFreeArgsMin(1); + NLastGetopt::TOptsParseResult optsRes(&opts, argc, argv); + SubstGlobal(append, "\\n", "\n"); SubstGlobal(prepend, "\\n", "\n"); TVector<TRec> recs; - const auto& files = optsRes.GetFreeArgs(); + const auto& files = optsRes.GetFreeArgs(); TVector<TStringBuf> keys; if (forceKeys.size()) diff --git a/tools/archiver/tests/directory/file1 b/tools/archiver/tests/directory/file1 index ac9b5ed12b..e2129701f1 100644 --- a/tools/archiver/tests/directory/file1 +++ b/tools/archiver/tests/directory/file1 @@ -1 +1 @@ -file1 +file1 diff --git a/tools/archiver/tests/directory/file2 b/tools/archiver/tests/directory/file2 index c253e57a47..6c493ff740 100644 --- a/tools/archiver/tests/directory/file2 +++ b/tools/archiver/tests/directory/file2 @@ -1 +1 @@ -file2 +file2 diff --git a/tools/archiver/tests/test.py b/tools/archiver/tests/test.py index 9a8a9c4e14..b92d58f6a9 100644 --- a/tools/archiver/tests/test.py +++ b/tools/archiver/tests/test.py @@ -1,41 +1,41 @@ -import os -import logging -from yatest import common as ytc - -logger = logging.getLogger("test_logger") - - -class TestArchiver(object): - @classmethod - def setup_class(cls): +import os +import logging +from yatest import common as ytc + +logger = logging.getLogger("test_logger") + + +class TestArchiver(object): + @classmethod + def setup_class(cls): cls.archiver_path = ytc.binary_path("tools/archiver/archiver") - - def test_recursive(self): - assert 'archiver' == os.path.basename(self.archiver_path) - assert os.path.exists(self.archiver_path) - contents = ytc.source_path("tools/archiver/tests/directory") - ytc.execute( - command=[ - self.archiver_path, - "--output", "archive", - "--recursive", - contents, - ] - ) - with open('result', 'w') as archive_list: - ytc.execute( - command=[ - self.archiver_path, - "--list", - "archive", - ], - stdout=archive_list, - stderr=None, - ) - archive_list = sorted(open('result').read().strip().split('\n')) + + def test_recursive(self): + assert 'archiver' == os.path.basename(self.archiver_path) + assert os.path.exists(self.archiver_path) + contents = ytc.source_path("tools/archiver/tests/directory") + ytc.execute( + command=[ + self.archiver_path, + "--output", "archive", + "--recursive", + contents, + ] + ) + with open('result', 'w') as archive_list: + ytc.execute( + command=[ + self.archiver_path, + "--list", + "archive", + ], + stdout=archive_list, + stderr=None, + ) + archive_list = sorted(open('result').read().strip().split('\n')) assert len(archive_list) == 3 - assert archive_list[0] == 'file1' - assert archive_list[1] == 'file2' + assert archive_list[0] == 'file1' + assert archive_list[1] == 'file2' assert archive_list[2] == 'file3' def test_deduplicate(self): diff --git a/tools/archiver/tests/ya.make b/tools/archiver/tests/ya.make index 76b276aa12..445b4d3e70 100644 --- a/tools/archiver/tests/ya.make +++ b/tools/archiver/tests/ya.make @@ -1,9 +1,9 @@ OWNER(mvel) - + PY2TEST() - + TEST_SRCS(test.py) - + DATA(arcadia/tools/archiver/tests) DEPENDS(tools/archiver) diff --git a/tools/archiver/ya.make b/tools/archiver/ya.make index 07aac3a635..757378c1b3 100644 --- a/tools/archiver/ya.make +++ b/tools/archiver/ya.make @@ -1,9 +1,9 @@ PROGRAM() -OWNER( - pg - mvel -) +OWNER( + pg + mvel +) PEERDIR( library/cpp/archive diff --git a/tools/enum_parser/enum_parser/main.cpp b/tools/enum_parser/enum_parser/main.cpp index fca1161b6e..0943c69c1d 100644 --- a/tools/enum_parser/enum_parser/main.cpp +++ b/tools/enum_parser/enum_parser/main.cpp @@ -1,42 +1,42 @@ #include <library/cpp/json/writer/json_value.h> #include <library/cpp/json/writer/json.h> #include <library/cpp/getopt/small/last_getopt.h> - -#include <tools/enum_parser/parse_enum/parse_enum.h> - -#include <util/stream/file.h> -#include <util/stream/output.h> -#include <util/stream/input.h> -#include <util/stream/mem.h> - -#include <util/charset/wide.h> + +#include <tools/enum_parser/parse_enum/parse_enum.h> + +#include <util/stream/file.h> +#include <util/stream/output.h> +#include <util/stream/input.h> +#include <util/stream/mem.h> + +#include <util/charset/wide.h> #include <util/string/builder.h> -#include <util/string/strip.h> -#include <util/string/cast.h> +#include <util/string/strip.h> +#include <util/string/cast.h> #include <util/string/join.h> -#include <util/string/subst.h> -#include <util/generic/map.h> +#include <util/string/subst.h> +#include <util/generic/map.h> #include <util/generic/string.h> -#include <util/generic/vector.h> -#include <util/generic/ptr.h> -#include <util/generic/yexception.h> -#include <util/generic/maybe.h> -#include <util/system/fs.h> -#include <util/folder/path.h> - +#include <util/generic/vector.h> +#include <util/generic/ptr.h> +#include <util/generic/yexception.h> +#include <util/generic/maybe.h> +#include <util/system/fs.h> +#include <util/folder/path.h> + void WriteHeader(const TString& headerName, IOutputStream& out, IOutputStream* headerOutPtr = nullptr) { - out << "// This file was auto-generated. Do not edit!!!\n"; + out << "// This file was auto-generated. Do not edit!!!\n"; out << "#include " << headerName << "\n"; out << "#include <tools/enum_parser/enum_serialization_runtime/enum_runtime.h>\n\n"; out << "#include <tools/enum_parser/enum_parser/stdlib_deps.h>\n\n"; - out << "#include <util/generic/typetraits.h>\n"; - out << "#include <util/generic/singleton.h>\n"; + out << "#include <util/generic/typetraits.h>\n"; + out << "#include <util/generic/singleton.h>\n"; out << "#include <util/generic/string.h>\n"; out << "#include <util/generic/vector.h>\n"; - out << "#include <util/generic/map.h>\n"; + out << "#include <util/generic/map.h>\n"; out << "#include <util/generic/serialized_enum.h>\n"; - out << "#include <util/string/cast.h>\n"; - out << "#include <util/stream/output.h>\n\n"; + out << "#include <util/string/cast.h>\n"; + out << "#include <util/stream/output.h>\n\n"; if (headerOutPtr) { auto& outHeader = *headerOutPtr; @@ -45,182 +45,182 @@ void WriteHeader(const TString& headerName, IOutputStream& out, IOutputStream* h outHeader << "#include <util/generic/serialized_enum.h>\n"; outHeader << "#include " << headerName << "\n"; } -} - +} + static inline void JsonEscape(TString& s) { - SubstGlobal(s, "\\", "\\\\"); - SubstGlobal(s, "\"", "\\\""); - SubstGlobal(s, "\r", "\\r"); - SubstGlobal(s, "\n", "\\n"); - SubstGlobal(s, "\t", "\\t"); -} - + SubstGlobal(s, "\\", "\\\\"); + SubstGlobal(s, "\"", "\\\""); + SubstGlobal(s, "\r", "\\r"); + SubstGlobal(s, "\n", "\\n"); + SubstGlobal(s, "\t", "\\t"); +} + static inline TString JsonQuote(const TString& s) { TString quoted = s; - JsonEscape(quoted); - return "\"" + quoted + "\""; // do not use .Quote() here, it performs escaping! -} - - -/// Simplifed JSON map encoder for generic types -template<typename T> + JsonEscape(quoted); + return "\"" + quoted + "\""; // do not use .Quote() here, it performs escaping! +} + + +/// Simplifed JSON map encoder for generic types +template<typename T> void OutKey(IOutputStream& out, const TString& key, const T& value, bool escape = true) { TString quoted = ToString(value); - if (escape) { - quoted = JsonQuote(quoted); - } - out << "\"" << key << "\": " << quoted << ",\n"; -} - -/// Simplifed JSON map encoder for TMaybe + if (escape) { + quoted = JsonQuote(quoted); + } + out << "\"" << key << "\": " << quoted << ",\n"; +} + +/// Simplifed JSON map encoder for TMaybe void OutKey(IOutputStream& out, const TString& key, const TMaybe<TString>& value) { TString quoted; - if (value) { - quoted = JsonQuote(ToString(*value)); - } else { - quoted = "null"; - } - out << "\"" << key << "\": " << quoted << ",\n"; -} - - -/// Simplifed JSON map encoder for bool values + if (value) { + quoted = JsonQuote(ToString(*value)); + } else { + quoted = "null"; + } + out << "\"" << key << "\": " << quoted << ",\n"; +} + + +/// Simplifed JSON map encoder for bool values void OutKey(IOutputStream& out, const TString& key, const bool& value) { - out << "\"" << key << "\": " << (value ? "true" : "false") << ",\n"; -} - - -/// Simplifed JSON map encoder for array items -template<typename T> + out << "\"" << key << "\": " << (value ? "true" : "false") << ",\n"; +} + + +/// Simplifed JSON map encoder for array items +template<typename T> void OutItem(IOutputStream& out, const T& value, bool escape = true) { TString quoted = ToString(value); - if (escape) { - quoted = JsonQuote(quoted); - } - out << quoted << ",\n"; -} - -/// Cut trailing ",\n" or "," -static inline void FinishItems(TStringStream& out) { + if (escape) { + quoted = JsonQuote(quoted); + } + out << quoted << ",\n"; +} + +/// Cut trailing ",\n" or "," +static inline void FinishItems(TStringStream& out) { TString& s = out.Str(); - if (s.EndsWith(",\n")) { + if (s.EndsWith(",\n")) { s.remove(s.size() - 2, 2); - } - if (s.EndsWith(",")) { - s.pop_back(); - } -} - - -static inline void OpenMap(TStringStream& out) { - out << "{\n"; -} - -static inline void CloseMap(TStringStream& out) { - out << "}\n"; -} - -static inline void OpenArray(TStringStream& out) { - out << "[\n"; -} - -static inline void CloseArray(TStringStream& out) { - out << "]\n"; -} - + } + if (s.EndsWith(",")) { + s.pop_back(); + } +} + + +static inline void OpenMap(TStringStream& out) { + out << "{\n"; +} + +static inline void CloseMap(TStringStream& out) { + out << "}\n"; +} + +static inline void OpenArray(TStringStream& out) { + out << "[\n"; +} + +static inline void CloseArray(TStringStream& out) { + out << "]\n"; +} + static TString WrapStringBuf(const TStringBuf str) { return TString::Join("TStringBuf(\"", str, "\")"); } -void GenerateEnum( - const TEnumParser::TEnum& en, +void GenerateEnum( + const TEnumParser::TEnum& en, IOutputStream& out, IOutputStream* jsonEnumOut = nullptr, IOutputStream* headerOutPtr = nullptr -) { - TStringStream jEnum; - OpenMap(jEnum); - +) { + TStringStream jEnum; + OpenMap(jEnum); + size_t count = en.Items.size(); - OutKey(jEnum, "count", count); + OutKey(jEnum, "count", count); const TString name = TEnumParser::ScopeStr(en.Scope) + en.CppName; - OutKey(jEnum, "full_name", name); - OutKey(jEnum, "cpp_name", en.CppName); - TStringStream scopeJson; - OpenArray(scopeJson); - for (const auto& scopeItem : en.Scope) { - OutItem(scopeJson, scopeItem); - } - FinishItems(scopeJson); - CloseArray(scopeJson); - - OutKey(jEnum, "scope", scopeJson.Str(), false); - OutKey(jEnum, "enum_class", en.EnumClass); - - TEnumParser::TScope outerScope = en.Scope; - if (en.EnumClass) { - outerScope.push_back(en.CppName); - } - + OutKey(jEnum, "full_name", name); + OutKey(jEnum, "cpp_name", en.CppName); + TStringStream scopeJson; + OpenArray(scopeJson); + for (const auto& scopeItem : en.Scope) { + OutItem(scopeJson, scopeItem); + } + FinishItems(scopeJson); + CloseArray(scopeJson); + + OutKey(jEnum, "scope", scopeJson.Str(), false); + OutKey(jEnum, "enum_class", en.EnumClass); + + TEnumParser::TScope outerScope = en.Scope; + if (en.EnumClass) { + outerScope.push_back(en.CppName); + } + TString outerScopeStr = TEnumParser::ScopeStr(outerScope); - + TString cName = name; - SubstGlobal(cName, "::", ""); - - out << "// I/O for " << name << "\n"; - + SubstGlobal(cName, "::", ""); + + out << "// I/O for " << name << "\n"; + TString nsName = "N" + cName + "Private"; - - out << "namespace { namespace " << nsName << " {\n"; - + + out << "namespace { namespace " << nsName << " {\n"; + TVector<TString> nameInitializerPairs; TVector<TString> valueInitializerPairs; TVector<TString> cppNamesInitializer; - - TStringStream jItems; - OpenArray(jItems); - - for (const auto& it : en.Items) { - TStringStream jEnumItem; - OpenMap(jEnumItem); - - OutKey(jEnumItem, "cpp_name", it.CppName); - OutKey(jEnumItem, "value", it.Value); - OutKey(jEnumItem, "comment_text", it.CommentText); - - TStringStream jAliases; - OpenArray(jAliases); - + + TStringStream jItems; + OpenArray(jItems); + + for (const auto& it : en.Items) { + TStringStream jEnumItem; + OpenMap(jEnumItem); + + OutKey(jEnumItem, "cpp_name", it.CppName); + OutKey(jEnumItem, "value", it.Value); + OutKey(jEnumItem, "comment_text", it.CommentText); + + TStringStream jAliases; + OpenArray(jAliases); + TString strValue = it.CppName; - if (it.Aliases) { - // first alias is main - strValue = it.Aliases[0]; - OutKey(jEnumItem, "str_value", strValue); - } + if (it.Aliases) { + // first alias is main + strValue = it.Aliases[0]; + OutKey(jEnumItem, "str_value", strValue); + } nameInitializerPairs.push_back("TNameBufs::EnumStringPair(" + outerScopeStr + it.CppName + ", " + WrapStringBuf(strValue) + ")"); cppNamesInitializer.push_back(WrapStringBuf(it.CppName)); - - for (const auto& alias : it.Aliases) { + + for (const auto& alias : it.Aliases) { valueInitializerPairs.push_back("TNameBufs::EnumStringPair(" + outerScopeStr + it.CppName + ", " + WrapStringBuf(alias) + ")"); - OutItem(jAliases, alias); - } - FinishItems(jAliases); - CloseArray(jAliases); - - if (!it.Aliases) { + OutItem(jAliases, alias); + } + FinishItems(jAliases); + CloseArray(jAliases); + + if (!it.Aliases) { valueInitializerPairs.push_back("TNameBufs::EnumStringPair(" + outerScopeStr + it.CppName + ", " + WrapStringBuf(it.CppName) + ")"); - } - OutKey(jEnumItem, "aliases", jAliases.Str(), false); - - FinishItems(jEnumItem); - CloseMap(jEnumItem); - - OutItem(jItems, jEnumItem.Str(), false); - } - FinishItems(jItems); - CloseArray(jItems); - OutKey(jEnum, "items", jItems.Str(), false); - + } + OutKey(jEnumItem, "aliases", jAliases.Str(), false); + + FinishItems(jEnumItem); + CloseMap(jEnumItem); + + OutItem(jItems, jEnumItem.Str(), false); + } + FinishItems(jItems); + CloseArray(jItems); + OutKey(jEnum, "items", jItems.Str(), false); + auto defineConstArray = [&out, payloadCache = TMap<std::pair<TString, TVector<TString>>, TString>()](const TStringBuf indent, const TStringBuf elementType, const TStringBuf name, const TVector<TString>& items) mutable { if (items.empty()) { // ISO C++ forbids zero-size array out << indent << "static constexpr const TArrayRef<const " << elementType << "> " << name << ";\n"; @@ -244,7 +244,7 @@ void GenerateEnum( out << " public:\n"; out << " using TBase = ::NEnumSerializationRuntime::TEnumDescription<" << name << ">;\n\n"; out << " inline TNameBufs();\n\n"; - + // Instance out << " static inline const TNameBufs& Instance() {\n"; out << " return *SingletonWithPriority<TNameBufs, 0>();\n"; // destroy enum serializers last, because it may be used from destructor of another global object @@ -269,60 +269,60 @@ void GenerateEnum( out << " : TBase(ENUM_INITIALIZATION_DATA)\n"; out << " {\n"; out << " }\n\n"; - - out << "}}\n\n"; - + + out << "}}\n\n"; + if (headerOutPtr) { (*headerOutPtr) << "// I/O for " << name << "\n"; } - // outer ToString + // outer ToString if (headerOutPtr) { (*headerOutPtr) << "const TString& ToString(" << name << ");\n"; } out << "const TString& ToString(" << name << " x) {\n"; - out << " const " << nsName << "::TNameBufs& names = " << nsName << "::TNameBufs::Instance();\n"; - out << " return names.ToString(x);\n"; - out << "}\n\n"; - - // outer FromString + out << " const " << nsName << "::TNameBufs& names = " << nsName << "::TNameBufs::Instance();\n"; + out << " return names.ToString(x);\n"; + out << "}\n\n"; + + // outer FromString if (headerOutPtr) { (*headerOutPtr) << "bool FromString(const TString& name, " << name << "& ret);\n"; } out << "bool FromString(const TString& name, " << name << "& ret) {\n"; - out << " const " << nsName << "::TNameBufs& names = " << nsName << "::TNameBufs::Instance();\n"; - out << " return names.FromString(name, ret);\n"; - out << "}\n\n"; - - // outer FromString + out << " const " << nsName << "::TNameBufs& names = " << nsName << "::TNameBufs::Instance();\n"; + out << " return names.FromString(name, ret);\n"; + out << "}\n\n"; + + // outer FromString if (headerOutPtr) { (*headerOutPtr) << "bool FromString(const TStringBuf& name, " << name << "& ret);\n"; } - out << "bool FromString(const TStringBuf& name, " << name << "& ret) {\n"; - out << " const " << nsName << "::TNameBufs& names = " << nsName << "::TNameBufs::Instance();\n"; - out << " return names.FromString(name, ret);\n"; - out << "}\n\n"; - - // specialization for internal FromStringImpl - out << "template<>\n"; - out << name << " FromStringImpl<" << name << ">(const char* data, size_t len) {\n"; - out << " const " << nsName << "::TNameBufs& names = " << nsName << "::TNameBufs::Instance();\n"; - out << " return names.FromString(TStringBuf(data, len));\n"; - out << "}\n\n"; - - // specialization for internal TryFromStringImpl - out << "template<>\n"; - out << "bool TryFromStringImpl<" << name << ">(const char* data, size_t len, " << name << "& result) {\n"; - out << " return FromString(TStringBuf(data, len), result);\n"; - out << "}\n\n"; - - // outer Out - out << "template<>\n"; + out << "bool FromString(const TStringBuf& name, " << name << "& ret) {\n"; + out << " const " << nsName << "::TNameBufs& names = " << nsName << "::TNameBufs::Instance();\n"; + out << " return names.FromString(name, ret);\n"; + out << "}\n\n"; + + // specialization for internal FromStringImpl + out << "template<>\n"; + out << name << " FromStringImpl<" << name << ">(const char* data, size_t len) {\n"; + out << " const " << nsName << "::TNameBufs& names = " << nsName << "::TNameBufs::Instance();\n"; + out << " return names.FromString(TStringBuf(data, len));\n"; + out << "}\n\n"; + + // specialization for internal TryFromStringImpl + out << "template<>\n"; + out << "bool TryFromStringImpl<" << name << ">(const char* data, size_t len, " << name << "& result) {\n"; + out << " return FromString(TStringBuf(data, len), result);\n"; + out << "}\n\n"; + + // outer Out + out << "template<>\n"; out << "void Out<" << name << ">(IOutputStream& os, TTypeTraits<" << name << ">::TFuncParam n) {\n"; out << " const " << nsName << "::TNameBufs& names = " << nsName << "::TNameBufs::Instance();\n"; out << " return names.Out(&os, n);\n"; - out << "}\n\n"; - + out << "}\n\n"; + // specializations for NEnumSerializationRuntime function family out << "namespace NEnumSerializationRuntime {\n"; // template<> GetEnumAllValues @@ -338,7 +338,7 @@ void GenerateEnum( out << " const " << nsName << "::TNameBufs& names = " << nsName << "::TNameBufs::Instance();\n"; out << " return names.AllEnumNames();\n"; out << " }\n\n"; - + // template<> GetEnumNames<EnumType> out << " template<>\n"; out << " TMappedDictView<" << name << ", TString> GetEnumNamesImpl<" << name << ">() {\n"; @@ -353,8 +353,8 @@ void GenerateEnum( out << " return names.AllEnumCppNames();\n"; out << " }\n"; - out << "}\n\n"; - + out << "}\n\n"; + if (headerOutPtr) { // <EnumType>Count auto& outHeader = *headerOutPtr; @@ -363,30 +363,30 @@ void GenerateEnum( outHeader << " return " << en.Items.size() << ";\n"; outHeader << "}\n"; } - - FinishItems(jEnum); - jEnum << "}\n"; - - if (jsonEnumOut) { - *jsonEnumOut << jEnum.Str(); - } -} - -int main(int argc, char** argv) { - try { - using namespace NLastGetopt; - TOpts opts = NLastGetopt::TOpts::Default(); - opts.AddHelpOption(); - + + FinishItems(jEnum); + jEnum << "}\n"; + + if (jsonEnumOut) { + *jsonEnumOut << jEnum.Str(); + } +} + +int main(int argc, char** argv) { + try { + using namespace NLastGetopt; + TOpts opts = NLastGetopt::TOpts::Default(); + opts.AddHelpOption(); + TString outputFileName; TString outputHeaderFileName; TString outputJsonFileName; TString includePath; - opts.AddLongOption('o', "output").OptionalArgument("<output-file>").StoreResult(&outputFileName) - .Help( - "Output generated code to specified file.\n" - "When not set, standard output is used." - ); + opts.AddLongOption('o', "output").OptionalArgument("<output-file>").StoreResult(&outputFileName) + .Help( + "Output generated code to specified file.\n" + "When not set, standard output is used." + ); opts.AddLongOption('h', "header").OptionalArgument("<output-header>").StoreResult(&outputHeaderFileName) .Help( "Generate appropriate header to specified file.\n" @@ -397,75 +397,75 @@ int main(int argc, char** argv) { "Include input header using this path in angle brackets.\n" "When not set, header basename is used in double quotes." ); - - opts.AddLongOption('j', "json-output").OptionalArgument("<json-output>").StoreResult(&outputJsonFileName) - .Help( - "Generate enum data in JSON format." - ); - - opts.SetFreeArgsNum(1); - opts.SetFreeArgTitle(0, "<input-file>", "Input header file with enum declarations"); - - TOptsParseResult res(&opts, argc, argv); - + + opts.AddLongOption('j', "json-output").OptionalArgument("<json-output>").StoreResult(&outputJsonFileName) + .Help( + "Generate enum data in JSON format." + ); + + opts.SetFreeArgsNum(1); + opts.SetFreeArgTitle(0, "<input-file>", "Input header file with enum declarations"); + + TOptsParseResult res(&opts, argc, argv); + TVector<TString> freeArgs = res.GetFreeArgs(); TString inputFileName = freeArgs[0]; - + THolder<IOutputStream> hOut; IOutputStream* out = &Cout; - + THolder<IOutputStream> headerOut; THolder<IOutputStream> jsonOut; - - - if (outputFileName) { + + + if (outputFileName) { NFs::Remove(outputFileName); hOut.Reset(new TFileOutput(outputFileName)); - out = hOut.Get(); + out = hOut.Get(); if (outputHeaderFileName) { headerOut.Reset(new TFileOutput(outputHeaderFileName)); } - - if (outputJsonFileName) { + + if (outputJsonFileName) { jsonOut.Reset(new TFileOutput(outputJsonFileName)); - } - } - + } + } + if (!includePath) { includePath = TString() + '"' + TFsPath(inputFileName).Basename() + '"'; } else { includePath = TString() + '<' + includePath + '>'; } - TEnumParser parser(inputFileName); - WriteHeader(includePath, *out, headerOut.Get()); - - TStringStream jEnums; - OpenArray(jEnums); - - for (const auto& en : parser.Enums) { - if (!en.CppName) { - // skip unnamed enum declarations - continue; - } - - TStringStream jEnum; - GenerateEnum(en, *out, &jEnum, headerOut.Get()); - OutItem(jEnums, jEnum.Str(), false); - } - FinishItems(jEnums); - CloseArray(jEnums); - - if (jsonOut) { - *jsonOut << jEnums.Str() << Endl; - } - - return 0; - } catch (...) { - Cerr << CurrentExceptionMessage() << Endl; - } - - return 1; -} + TEnumParser parser(inputFileName); + WriteHeader(includePath, *out, headerOut.Get()); + + TStringStream jEnums; + OpenArray(jEnums); + + for (const auto& en : parser.Enums) { + if (!en.CppName) { + // skip unnamed enum declarations + continue; + } + + TStringStream jEnum; + GenerateEnum(en, *out, &jEnum, headerOut.Get()); + OutItem(jEnums, jEnum.Str(), false); + } + FinishItems(jEnums); + CloseArray(jEnums); + + if (jsonOut) { + *jsonOut << jEnums.Str() << Endl; + } + + return 0; + } catch (...) { + Cerr << CurrentExceptionMessage() << Endl; + } + + return 1; +} diff --git a/tools/enum_parser/enum_parser/ya.make b/tools/enum_parser/enum_parser/ya.make index 8e7b19358f..82bac4e0f7 100644 --- a/tools/enum_parser/enum_parser/ya.make +++ b/tools/enum_parser/enum_parser/ya.make @@ -1,12 +1,12 @@ -OWNER( +OWNER( g:util - mvel -) - + mvel +) + IF (USE_PREBUILT_TOOLS) INCLUDE(${ARCADIA_ROOT}/build/prebuilt/tools/enum_parser/enum_parser/ya.make.prebuilt) ENDIF() - + IF (NOT PREBUILT) INCLUDE(${ARCADIA_ROOT}/tools/enum_parser/enum_parser/bin/ya.make) ENDIF() diff --git a/tools/enum_parser/ya.make b/tools/enum_parser/ya.make index 56c0286b82..762b282b3e 100644 --- a/tools/enum_parser/ya.make +++ b/tools/enum_parser/ya.make @@ -1,6 +1,6 @@ -RECURSE( - parse_enum +RECURSE( + parse_enum parse_enum/ut - enum_parser + enum_parser enum_serialization_runtime -) +) diff --git a/tools/ya.make b/tools/ya.make index 24d69b69e1..51a6b8b426 100644 --- a/tools/ya.make +++ b/tools/ya.make @@ -3,14 +3,14 @@ RECURSE( arcdocidstrip archiver archiver/alignment_test - archiver/tests + archiver/tests base64 bigram_compiler blender bmdump bstr c++filt - calendar_extractor + calendar_extractor check_formula_md5 check_json check_yaml @@ -35,7 +35,7 @@ RECURSE( dsindexer easyparser enforce_fsync - enum_parser + enum_parser event2cpp fast_diff fastcksum @@ -166,7 +166,7 @@ RECURSE( uc unpackrichtree unpackrrr - unpack_staff_cert + unpack_staff_cert untranslit_test untranslit_test/tests url @@ -186,4 +186,4 @@ IF (NOT OS_WINDOWS) RECURSE( netgdb ) -ENDIF() +ENDIF() |