xref: /llvm-project/clang/lib/ExtractAPI/API.cpp (revision 89f6b26f1beb2c1344f5cfeb34e405128544c76b)
1 //===- ExtractAPI/API.cpp ---------------------------------------*- C++ -*-===//
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 /// \file
10 /// This file implements the APIRecord and derived record structs,
11 /// and the APISet class.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "clang/ExtractAPI/API.h"
16 #include "clang/AST/CommentCommandTraits.h"
17 #include "clang/AST/CommentLexer.h"
18 #include "clang/AST/RawCommentList.h"
19 #include "clang/Index/USRGeneration.h"
20 #include "llvm/Support/Allocator.h"
21 
22 using namespace clang::extractapi;
23 using namespace llvm;
24 
25 GlobalRecord *APISet::addGlobal(GVKind Kind, StringRef Name, StringRef USR,
26                                 PresumedLoc Loc,
27                                 const AvailabilityInfo &Availability,
28                                 LinkageInfo Linkage, const DocComment &Comment,
29                                 DeclarationFragments Fragments,
30                                 DeclarationFragments SubHeading,
31                                 FunctionSignature Signature) {
32   auto Result = Globals.insert({Name, nullptr});
33   if (Result.second) {
34     // Create the record if it does not already exist.
35     auto Record = APIRecordUniquePtr<GlobalRecord>(new (Allocator) GlobalRecord{
36         Kind, Name, USR, Loc, Availability, Linkage, Comment, Fragments,
37         SubHeading, Signature});
38     Result.first->second = std::move(Record);
39   }
40   return Result.first->second.get();
41 }
42 
43 GlobalRecord *
44 APISet::addGlobalVar(StringRef Name, StringRef USR, PresumedLoc Loc,
45                      const AvailabilityInfo &Availability, LinkageInfo Linkage,
46                      const DocComment &Comment, DeclarationFragments Fragments,
47                      DeclarationFragments SubHeading) {
48   return addGlobal(GVKind::Variable, Name, USR, Loc, Availability, Linkage,
49                    Comment, Fragments, SubHeading, {});
50 }
51 
52 GlobalRecord *
53 APISet::addFunction(StringRef Name, StringRef USR, PresumedLoc Loc,
54                     const AvailabilityInfo &Availability, LinkageInfo Linkage,
55                     const DocComment &Comment, DeclarationFragments Fragments,
56                     DeclarationFragments SubHeading,
57                     FunctionSignature Signature) {
58   return addGlobal(GVKind::Function, Name, USR, Loc, Availability, Linkage,
59                    Comment, Fragments, SubHeading, Signature);
60 }
61 
62 StringRef APISet::recordUSR(const Decl *D) {
63   SmallString<128> USR;
64   index::generateUSRForDecl(D, USR);
65   return copyString(USR);
66 }
67 
68 StringRef APISet::copyString(StringRef String) {
69   if (String.empty())
70     return {};
71 
72   // No need to allocate memory and copy if the string has already been stored.
73   if (Allocator.identifyObject(String.data()))
74     return String;
75 
76   void *Ptr = Allocator.Allocate(String.size(), 1);
77   memcpy(Ptr, String.data(), String.size());
78   return StringRef(reinterpret_cast<const char *>(Ptr), String.size());
79 }
80 
81 APIRecord::~APIRecord() {}
82 
83 void GlobalRecord::anchor() {}
84