diff options
author | Steven Moreland <smoreland@google.com> | 2017-09-25 18:41:45 -0700 |
---|---|---|
committer | Steven Moreland <smoreland@google.com> | 2017-09-26 13:02:49 -0700 |
commit | f78c44db648a1f7dd38fed110b8e53cc04f96516 (patch) | |
tree | 25eddc5852e9980d0aee9ec867af381bcec5f764 /Coordinator.cpp | |
parent | 2f4ee8e8d5b4315646189b128e913e830c9803a8 (diff) | |
download | hidl-f78c44db648a1f7dd38fed110b8e53cc04f96516.tar.gz |
Coordinator add getFormatter.
This way Coordinator can keep track of the files
that are used and we can consolidate the code that
builds paths to output files.
Test: all host tests
Bug: 65738957
Change-Id: Iaf1b00e130be892ef1bb620017032c29140ee5b6
Diffstat (limited to 'Coordinator.cpp')
-rw-r--r-- | Coordinator.cpp | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/Coordinator.cpp b/Coordinator.cpp index a59cce9b..d08a8037 100644 --- a/Coordinator.cpp +++ b/Coordinator.cpp @@ -76,6 +76,50 @@ void Coordinator::addDefaultPackagePath(const std::string& root, const std::stri addPackagePath(root, path, nullptr /* error */); } +Formatter Coordinator::getFormatter(const std::string& outputPath, const FQName& fqName, + Location location, const std::string& fileName) const { + std::string filepath = getFilepath(outputPath, fqName, location, fileName); + + if (!Coordinator::MakeParentHierarchy(filepath)) { + fprintf(stderr, "ERROR: could not make directories for %s.\n", filepath.c_str()); + return Formatter::invalid(); + } + + FILE* file = fopen(filepath.c_str(), "w"); + + if (file == nullptr) { + fprintf(stderr, "ERROR: could not open file %s: %d\n", filepath.c_str(), errno); + return Formatter::invalid(); + } + + return Formatter(file); +} + +std::string Coordinator::getFilepath(const std::string& outputPath, const FQName& fqName, + Location location, const std::string& fileName) const { + std::string path = outputPath; + + switch (location) { + case Location::DIRECT: { /* nothing */ + } break; + case Location::PACKAGE_ROOT: { + path.append(getPackagePath(fqName, false /* relative */)); + } break; + case Location::GEN_OUTPUT: { + path.append(convertPackageRootToPath(fqName)); + path.append(getPackagePath(fqName, true /* relative */, false /* sanitized */)); + } break; + case Location::GEN_SANITIZED: { + path.append(convertPackageRootToPath(fqName)); + path.append(getPackagePath(fqName, true /* relative */, true /* sanitized */)); + } break; + default: { CHECK(false) << "Invalid location: " << static_cast<size_t>(location); } + } + + path.append(fileName); + return path; +} + AST* Coordinator::parse(const FQName& fqName, std::set<AST*>* parsedASTs, Enforce enforcement) const { CHECK(fqName.isFullyQualified()); |