13#include "clang/Tooling/CompilationDatabase.h"
14#include "llvm/ADT/StringExtras.h"
15#include "llvm/Support/Compression.h"
16#include "llvm/Support/Error.h"
17#include "llvm/Support/ScopedPrinter.h"
18#include "gmock/gmock.h"
19#include "gtest/gtest.h"
21#include <sys/resource.h>
24using ::testing::ElementsAre;
26using ::testing::UnorderedElementsAre;
27using ::testing::UnorderedElementsAreArray;
43 FileURI: file:///path/foo.h
51Documentation: 'Foo doc'
56 Directives: [ Include ]
59 Directives: [ Import ]
62 Directives: [ Include, Import ]
76 FileURI: file:///path/bar.h
85CompletionSnippetSuffix: '-snippet'
92 FileURI: file:///path/foo.cc
114URI: 'file:///path/source1.cpp'
116Digest: EED8F5EAF25C453C
118 - 'file:///path/inc1.h'
119 - 'file:///path/inc2.h'
125MATCHER_P3(IncludeHeaderWithRefAndDirectives, IncludeHeader,
References,
126 SupportedDirectives,
"") {
127 return (arg.IncludeHeader == IncludeHeader) &&
129 (arg.SupportedDirectives == SupportedDirectives);
136TEST(SerializationTest, NoCrashOnEmptyYAML) {
140TEST(SerializationTest, YAMLConversions) {
142 ASSERT_TRUE(
bool(ParsedYAML)) << ParsedYAML.takeError();
143 ASSERT_TRUE(
bool(ParsedYAML->Symbols));
145 *ParsedYAML->Symbols,
146 UnorderedElementsAre(
id(
"057557CEBF6E6B2D"),
id(
"057557CEBF6E6B2E")));
148 auto Sym1 = *ParsedYAML->Symbols->find(
150 auto Sym2 = *ParsedYAML->Symbols->find(
153 EXPECT_THAT(Sym1, qName(
"clang::Foo1"));
154 EXPECT_EQ(Sym1.Signature,
"");
155 EXPECT_EQ(Sym1.Documentation,
"Foo doc");
156 EXPECT_EQ(Sym1.ReturnType,
"int");
157 EXPECT_EQ(StringRef(Sym1.CanonicalDeclaration.FileURI),
"file:///path/foo.h");
159 EXPECT_EQ(
static_cast<uint8_t
>(Sym1.Flags), 129);
164 UnorderedElementsAre(
166 IncludeHeaderWithRefAndDirectives(
"include2", 3u,
Symbol::Import),
167 IncludeHeaderWithRefAndDirectives(
"include3", 2u,
171 EXPECT_THAT(Sym2, qName(
"clang::Foo2"));
172 EXPECT_EQ(Sym2.Signature,
"-sig");
173 EXPECT_EQ(Sym2.ReturnType,
"");
174 EXPECT_EQ(llvm::StringRef(Sym2.CanonicalDeclaration.FileURI),
175 "file:///path/bar.h");
179 ASSERT_TRUE(
bool(ParsedYAML->Refs));
183 ::testing::SizeIs(1))));
184 auto Ref1 = ParsedYAML->Refs->begin()->second.front();
186 EXPECT_EQ(StringRef(Ref1.Location.FileURI),
"file:///path/foo.cc");
190 ASSERT_TRUE(
bool(ParsedYAML->Relations));
192 *ParsedYAML->Relations,
195 ASSERT_TRUE(
bool(ParsedYAML->Cmd));
196 auto &Cmd = *ParsedYAML->Cmd;
197 ASSERT_EQ(Cmd.Directory,
"testdir");
198 EXPECT_THAT(Cmd.CommandLine, ElementsAre(
"cmd1",
"cmd2"));
200 ASSERT_TRUE(
bool(ParsedYAML->Sources));
201 const auto *URI =
"file:///path/source1.cpp";
202 ASSERT_TRUE(ParsedYAML->Sources->count(URI));
203 auto IGNDeserialized = ParsedYAML->Sources->lookup(URI);
204 EXPECT_EQ(llvm::toHex(IGNDeserialized.Digest),
"EED8F5EAF25C453C");
205 EXPECT_THAT(IGNDeserialized.DirectIncludes,
206 ElementsAre(
"file:///path/inc1.h",
"file:///path/inc2.h"));
207 EXPECT_EQ(IGNDeserialized.URI, URI);
211std::vector<std::string> yamlFromSymbols(
const SymbolSlab &Slab) {
212 std::vector<std::string> Result;
213 for (
const auto &Sym : Slab)
214 Result.push_back(
toYAML(Sym));
217std::vector<std::string> yamlFromRefs(
const RefSlab &Slab) {
218 std::vector<std::string> Result;
219 for (
const auto &Refs : Slab)
220 Result.push_back(
toYAML(Refs));
224std::vector<std::string> yamlFromRelations(
const RelationSlab &Slab) {
225 std::vector<std::string> Result;
226 for (
const auto &Rel : Slab)
227 Result.push_back(
toYAML(Rel));
231TEST(SerializationTest, BinaryConversions) {
233 EXPECT_TRUE(
bool(In)) << In.takeError();
236 IndexFileOut
Out(*In);
238 std::string Serialized = llvm::to_string(
Out);
241 ASSERT_TRUE(
bool(In2)) << In2.takeError();
242 ASSERT_TRUE(In2->Symbols);
243 ASSERT_TRUE(In2->Refs);
244 ASSERT_TRUE(In2->Relations);
247 EXPECT_THAT(yamlFromSymbols(*In2->Symbols),
248 UnorderedElementsAreArray(yamlFromSymbols(*In->Symbols)));
249 EXPECT_THAT(yamlFromRefs(*In2->Refs),
250 UnorderedElementsAreArray(yamlFromRefs(*In->Refs)));
251 EXPECT_THAT(yamlFromRelations(*In2->Relations),
252 UnorderedElementsAreArray(yamlFromRelations(*In->Relations)));
255TEST(SerializationTest, SrcsTest) {
257 EXPECT_TRUE(
bool(In)) << In.takeError();
259 std::string TestContent(
"TestContent");
260 IncludeGraphNode IGN;
261 IGN.Digest =
digest(TestContent);
262 IGN.DirectIncludes = {
"inc1",
"inc2"};
267 Sources[IGN.URI] = IGN;
269 IndexFileOut
Out(*In);
271 Out.Sources = &Sources;
273 std::string Serialized = llvm::to_string(
Out);
276 ASSERT_TRUE(
bool(In)) << In.takeError();
277 ASSERT_TRUE(In->Symbols);
278 ASSERT_TRUE(In->Refs);
279 ASSERT_TRUE(In->Sources);
280 ASSERT_TRUE(In->Sources->count(IGN.URI));
282 EXPECT_THAT(yamlFromSymbols(*In->Symbols),
283 UnorderedElementsAreArray(yamlFromSymbols(*In->Symbols)));
284 EXPECT_THAT(yamlFromRefs(*In->Refs),
285 UnorderedElementsAreArray(yamlFromRefs(*In->Refs)));
286 auto IGNDeserialized = In->Sources->lookup(IGN.URI);
287 EXPECT_EQ(IGNDeserialized.Digest, IGN.Digest);
288 EXPECT_EQ(IGNDeserialized.DirectIncludes, IGN.DirectIncludes);
289 EXPECT_EQ(IGNDeserialized.URI, IGN.URI);
290 EXPECT_EQ(IGNDeserialized.Flags, IGN.Flags);
294TEST(SerializationTest, CmdlTest) {
296 EXPECT_TRUE(
bool(In)) << In.takeError();
298 tooling::CompileCommand Cmd;
299 Cmd.Directory =
"testdir";
300 Cmd.CommandLine.push_back(
"cmd1");
301 Cmd.CommandLine.push_back(
"cmd2");
302 Cmd.Filename =
"ignored";
303 Cmd.Heuristic =
"ignored";
304 Cmd.Output =
"ignored";
306 IndexFileOut
Out(*In);
310 std::string Serialized = llvm::to_string(
Out);
313 ASSERT_TRUE(
bool(In)) << In.takeError();
314 ASSERT_TRUE(In->Cmd);
316 const tooling::CompileCommand &SerializedCmd = *In->Cmd;
317 EXPECT_EQ(SerializedCmd.CommandLine, Cmd.CommandLine);
318 EXPECT_EQ(SerializedCmd.Directory, Cmd.Directory);
319 EXPECT_NE(SerializedCmd.Filename, Cmd.Filename);
320 EXPECT_NE(SerializedCmd.Heuristic, Cmd.Heuristic);
321 EXPECT_NE(SerializedCmd.Output, Cmd.Output);
327#if LLVM_ON_UNIX && defined(RLIMIT_AS) && !LLVM_ADDRESS_SANITIZER_BUILD && \
328 !LLVM_MEMORY_SANITIZER_BUILD && !LLVM_THREAD_SANITIZER_BUILD
329class ScopedMemoryLimit {
330 struct rlimit OriginalLimit;
331 bool Succeeded =
false;
334 ScopedMemoryLimit(rlim_t Bytes) {
335 if (!getrlimit(RLIMIT_AS, &OriginalLimit)) {
336 struct rlimit NewLimit = OriginalLimit;
337 NewLimit.rlim_cur = Bytes;
338 Succeeded = !setrlimit(RLIMIT_AS, &NewLimit);
341 log(
"Failed to set rlimit");
344 ~ScopedMemoryLimit() {
346 setrlimit(RLIMIT_AS, &OriginalLimit);
350class ScopedMemoryLimit {
352 ScopedMemoryLimit(
unsigned Bytes) {
log(
"rlimit unsupported"); }
358TEST(SerializationTest, NoCrashOnBadArraySize) {
362 ASSERT_FALSE(!In) << In.takeError();
363 IndexFileOut
Out(*In);
365 std::string Serialized = llvm::to_string(
Out);
369 ASSERT_FALSE(!Parsed) << Parsed.takeError();
370 auto Srcs = llvm::find_if(Parsed->Chunks, [](riff::Chunk
C) {
371 return C.ID == riff::fourCC(
"srcs");
373 ASSERT_NE(Srcs, Parsed->Chunks.end());
384 std::string
FileDigest = llvm::fromHex(
"EED8F5EAF25C453C");
386 ASSERT_NE(
Pos, StringRef::npos) <<
"Couldn't locate file digest";
391 std::string CorruptSrcs =
392 (Srcs->Data.take_front(
Pos) + llvm::fromHex(
"ffffffff0f") +
393 "some_random_garbage")
395 Srcs->Data = CorruptSrcs;
398 ScopedMemoryLimit MemLimit(1000 * 1024 * 1024);
400 std::string CorruptFile = llvm::to_string(*Parsed);
402 ASSERT_TRUE(!CorruptParsed);
403 EXPECT_EQ(llvm::toString(CorruptParsed.takeError()),
404 "malformed or truncated include uri");
409TEST(SerializationTest, NoCrashOnBadStringTableSize) {
410 if (!llvm::compression::zlib::isAvailable()) {
411 log(
"skipping test, no zlib");
417 ASSERT_FALSE(!In) << In.takeError();
418 IndexFileOut
Out(*In);
420 std::string Serialized = llvm::to_string(
Out);
424 ASSERT_FALSE(!Parsed) << Parsed.takeError();
425 auto Stri = llvm::find_if(Parsed->Chunks, [](riff::Chunk
C) {
426 return C.ID == riff::fourCC(
"stri");
428 ASSERT_NE(Stri, Parsed->Chunks.end());
432 std::string CorruptStri =
433 (llvm::fromHex(
"ffffffff") + Stri->Data.drop_front(4)).str();
434 Stri->Data = CorruptStri;
435 std::string
FileDigest = llvm::fromHex(
"EED8F5EAF25C453C");
438 ScopedMemoryLimit MemLimit(1000 * 1024 * 1024);
440 std::string CorruptFile = llvm::to_string(*Parsed);
442 ASSERT_TRUE(!CorruptParsed);
443 EXPECT_THAT(llvm::toString(CorruptParsed.takeError()),
444 testing::HasSubstr(
"bytes is implausible"));
llvm::SmallString< 256U > Name
CompiledFragmentImpl & Out
static llvm::Expected< SymbolID > fromStr(llvm::StringRef)
llvm::Expected< File > readFile(llvm::StringRef Stream)
llvm::Expected< IndexFileIn > readIndexFile(llvm::StringRef Data, SymbolOrigin Origin)
FileDigest digest(llvm::StringRef Content)
std::array< uint8_t, 8 > FileDigest
TEST(BackgroundQueueTest, Priority)
llvm::StringMap< IncludeGraphNode > IncludeGraph
void log(const char *Fmt, Ts &&... Vals)
std::string toYAML(const Symbol &)
std::array< uint8_t, 20 > SymbolID
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
@ IndexedForCodeCompletion
Whether or not this symbol is meant to be used for the code completion.
@ Deprecated
Indicates if the symbol is deprecated.
@ Include
#include "header.h"
@ Import
#import "header.h"