Coverage Report

Created: 2020-02-25 14:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Tooling/ExpandResponseFilesCompilationDatabase.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- ExpandResponseFileCompilationDataBase.cpp --------------------------===//
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
#include "clang/Tooling/CompilationDatabase.h"
10
#include "llvm/ADT/StringRef.h"
11
#include "llvm/ADT/Triple.h"
12
#include "llvm/Support/CommandLine.h"
13
#include "llvm/Support/ConvertUTF.h"
14
#include "llvm/Support/ErrorOr.h"
15
#include "llvm/Support/MemoryBuffer.h"
16
#include "llvm/Support/Path.h"
17
#include "llvm/Support/StringSaver.h"
18
19
namespace clang {
20
namespace tooling {
21
namespace {
22
23
class ExpandResponseFilesDatabase : public CompilationDatabase {
24
public:
25
  ExpandResponseFilesDatabase(
26
      std::unique_ptr<CompilationDatabase> Base,
27
      llvm::cl::TokenizerCallback Tokenizer,
28
      llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
29
21
      : Base(std::move(Base)), Tokenizer(Tokenizer), FS(std::move(FS)) {
30
21
    assert(this->Base != nullptr);
31
21
    assert(this->Tokenizer != nullptr);
32
21
    assert(this->FS != nullptr);
33
21
  }
34
35
19
  std::vector<std::string> getAllFiles() const override {
36
19
    return Base->getAllFiles();
37
19
  }
38
39
  std::vector<CompileCommand>
40
18
  getCompileCommands(StringRef FilePath) const override {
41
18
    return expand(Base->getCompileCommands(FilePath));
42
18
  }
43
44
1
  std::vector<CompileCommand> getAllCompileCommands() const override {
45
1
    return expand(Base->getAllCompileCommands());
46
1
  }
47
48
private:
49
19
  std::vector<CompileCommand> expand(std::vector<CompileCommand> Cmds) const {
50
20
    for (auto &Cmd : Cmds) {
51
20
      bool SeenRSPFile = false;
52
20
      llvm::SmallVector<const char *, 20> Argv;
53
20
      Argv.reserve(Cmd.CommandLine.size());
54
84
      for (auto &Arg : Cmd.CommandLine) {
55
84
        Argv.push_back(Arg.c_str());
56
84
        SeenRSPFile |= Arg.front() == '@';
57
84
      }
58
20
      if (!SeenRSPFile)
59
19
        continue;
60
1
      llvm::BumpPtrAllocator Alloc;
61
1
      llvm::StringSaver Saver(Alloc);
62
1
      llvm::cl::ExpandResponseFiles(Saver, Tokenizer, Argv, false, false, *FS,
63
1
                                    llvm::StringRef(Cmd.Directory));
64
1
      // Don't assign directly, Argv aliases CommandLine.
65
1
      std::vector<std::string> ExpandedArgv(Argv.begin(), Argv.end());
66
1
      Cmd.CommandLine = std::move(ExpandedArgv);
67
1
    }
68
19
    return Cmds;
69
19
  }
70
71
private:
72
  std::unique_ptr<CompilationDatabase> Base;
73
  llvm::cl::TokenizerCallback Tokenizer;
74
  llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
75
};
76
77
} // namespace
78
79
std::unique_ptr<CompilationDatabase>
80
expandResponseFiles(std::unique_ptr<CompilationDatabase> Base,
81
21
                    llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS) {
82
21
  auto Tokenizer = llvm::Triple(llvm::sys::getProcessTriple()).isOSWindows()
83
21
                       ? 
llvm::cl::TokenizeWindowsCommandLine0
84
21
                       : llvm::cl::TokenizeGNUCommandLine;
85
21
  return std::make_unique<ExpandResponseFilesDatabase>(
86
21
      std::move(Base), Tokenizer, std::move(FS));
87
21
}
88
89
} // namespace tooling
90
} // namespace clang