Skip to content

Commit

Permalink
feat(cxx_indexer): include build/config fact on anchors when present (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
shahms committed Feb 5, 2019
1 parent 53e3a09 commit 96c7d6b
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 11 deletions.
1 change: 1 addition & 0 deletions kythe/cxx/common/indexing/KytheGraphRecorder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ static const std::string* const kPropertySpellings[] = {
new std::string("/kythe/details"),
new std::string("/kythe/context/url"),
new std::string("/kythe/doc/uri"),
new std::string("/kythe/build/config"),
};

static const std::string* const kEmptyStringSpelling = new std::string("");
Expand Down
3 changes: 2 additions & 1 deletion kythe/cxx/common/indexing/KytheGraphRecorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ enum class PropertyID {
kDiagnosticMessage,
kDiagnosticDetails,
kDiagnosticContextOrUrl,
kDocUri
kDocUri,
kBuildConfig
};

/// \brief Known edge kinds. See the schema for details.
Expand Down
1 change: 1 addition & 0 deletions kythe/cxx/indexer/cxx/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ cc_library(
"//kythe/cxx/extractor:index_pack",
"//kythe/cxx/extractor:supported_language",
"//kythe/proto:analysis_cc_proto",
"//kythe/proto:buildinfo_cc_proto",
"//kythe/proto:common_cc_proto",
"//kythe/proto:cxx_cc_proto",
"//kythe/proto:storage_cc_proto",
Expand Down
20 changes: 19 additions & 1 deletion kythe/cxx/indexer/cxx/IndexerFrontendAction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "kythe/cxx/indexer/cxx/KytheVFS.h"
#include "kythe/cxx/indexer/cxx/proto_conversions.h"
#include "kythe/proto/analysis.pb.h"
#include "kythe/proto/buildinfo.pb.h"
#include "kythe/proto/cxx.pb.h"
#include "llvm/ADT/Twine.h"
#include "third_party/llvm/src/clang_builtin_headers.h"
Expand All @@ -45,6 +46,10 @@ bool RunToolOnCode(std::unique_ptr<clang::FrontendAction> tool_action,

namespace {

// Message type URI for the build details message.
constexpr absl::string_view kBuildDetailsURI =
"kythe.io/proto/kythe.proto.BuildDetails";

/// \brief Range wrapper around unpacked ContextDependentVersion rows.
class FileContextRows {
public:
Expand Down Expand Up @@ -78,6 +83,18 @@ bool DecodeDetails(const proto::CompilationUnit& Unit,
return false;
}

std::string ExtractBuildConfig(const proto::CompilationUnit& Unit) {
proto::BuildDetails details;
for (const auto& Any : Unit.details()) {
if (Any.type_url() == kBuildDetailsURI) {
if (UnpackAny(Any, &details)) {
return details.build_config();
}
}
}
return "";
}

bool DecodeHeaderSearchInfo(const proto::CxxCompilationUnitDetails& Details,
HeaderSearchInfo& Info) {
if (!Details.has_header_search_info()) {
Expand Down Expand Up @@ -147,7 +164,8 @@ std::string IndexCompilationUnit(
new IndexVFS(FSO.WorkingDir, Files, Dirs));
KytheGraphRecorder Recorder(&Output);
KytheGraphObserver Observer(&Recorder, &Client, MetaSupports, VFS,
Options.ReportProfileEvent);
Options.ReportProfileEvent,
ExtractBuildConfig(Unit));
if (Cache != nullptr) {
Output.UseHashCache(Cache);
Observer.StopDeferringNodes();
Expand Down
7 changes: 7 additions & 0 deletions kythe/cxx/indexer/cxx/KytheGraphObserver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ kythe::proto::VName KytheGraphObserver::VNameFromRange(
}
}
out_name.set_language(supported_language::kIndexerLang);
if (!build_config_.empty()) {
absl::StrAppend(out_name.mutable_signature(), "%", build_config_);
}
out_name.set_signature(CompressString(out_name.signature()));
return out_name;
}
Expand Down Expand Up @@ -383,6 +386,10 @@ void KytheGraphObserver::UnconditionalRecordRange(
recorder_->AddEdge(anchor_name_ref, EdgeKindID::kChildOfContext,
VNameRefFromNodeId(range.Context));
}
if (!build_config_.empty()) {
recorder_->AddProperty(anchor_name_ref, PropertyID::kBuildConfig,
build_config_);
}
}

void KytheGraphObserver::MetaHookDefines(const MetadataFile& meta,
Expand Down
15 changes: 10 additions & 5 deletions kythe/cxx/indexer/cxx/KytheGraphObserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,17 @@ class KytheClaimToken : public GraphObserver::ClaimToken {
/// discovered during indexing to the provided `KytheGraphRecorder`.
class KytheGraphObserver : public GraphObserver {
public:
KytheGraphObserver(KytheGraphRecorder* recorder, KytheClaimClient* client,
const MetadataSupports* meta_supports,
const llvm::IntrusiveRefCntPtr<IndexVFS>& vfs,
ProfilingCallback ReportProfileEventCallback)
explicit KytheGraphObserver(KytheGraphRecorder* recorder,
KytheClaimClient* client,
const MetadataSupports* meta_supports,
const llvm::IntrusiveRefCntPtr<IndexVFS>& vfs,
ProfilingCallback ReportProfileEventCallback,
std::string build_config = "")
: recorder_(CHECK_NOTNULL(recorder)),
client_(CHECK_NOTNULL(client)),
meta_supports_(CHECK_NOTNULL(meta_supports)),
vfs_(vfs) {
vfs_(vfs),
build_config_(std::move(build_config)) {
default_token_.set_rough_claimed(true);
type_token_.set_rough_claimed(true);
ReportProfileEvent = std::move(ReportProfileEventCallback);
Expand Down Expand Up @@ -602,6 +605,8 @@ class KytheGraphObserver : public GraphObserver {
KytheClaimToken default_token_;
/// The claim token to use for structural types.
KytheClaimToken type_token_;
/// Name of the platform or build configuration to emit on anchors.
const std::string build_config_;
/// Information about builtin nodes.
struct Builtin {
/// This Builtin's NodeId.
Expand Down
17 changes: 13 additions & 4 deletions kythe/cxx/indexer/cxx/frontend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ DEFINE_int32(min_size, 4096, "Minimum size of an entry bundle");
DEFINE_int32(max_size, 1024 * 32, "Maximum size of an entry bundle");
DEFINE_bool(cache_stats, false, "Show cache stats");
DEFINE_string(icorpus, "", "Corpus to use for files specified with -i");
DEFINE_string(ibuild_config, "",
"Build config to use for files specified with -i");
DEFINE_bool(normalize_file_vnames, false, "Normalize incoming file vnames.");
DEFINE_string(experimental_dynamic_claim_cache, "",
"Use a memcache instance for dynamic claims (EXPERIMENTAL)");
Expand All @@ -66,6 +68,10 @@ namespace {
/// The prefix prepended to silent inputs. Only checked when "--test_claim"
/// is enabled.
constexpr char kSilentPrefix[] = "silent:";

/// The message type URI for the build details message.
constexpr char kBuildDetailsURI[] = "kythe.io/proto/kythe.proto.BuildDetails";

/// \return the input name stripped of its prefix if it's silent; an empty
/// string otherwise.
llvm::StringRef strip_silent_input_prefix(llvm::StringRef argument) {
Expand Down Expand Up @@ -293,6 +299,13 @@ void IndexerContext::LoadDataFromUnpackedFile(
job.unit.add_argument(arg);
}
job.unit.mutable_v_name()->set_corpus(FLAGS_icorpus);
if (!FLAGS_ibuild_config.empty()) {
proto::BuildDetails details;
details.set_build_config(FLAGS_ibuild_config);
auto* any = job.unit.add_details();
any->PackFrom(details);
any->set_type_url(kBuildDetailsURI);
}
MaybeNormalizeFileVNames(&job);
visit(job);
}
Expand Down Expand Up @@ -372,10 +385,6 @@ IndexerContext::~IndexerContext() { CloseOutputStreams(); }

void IndexerContext::EnumerateCompilations(
const CompilationVisitCallback& visit) {
// This forces the BuildDetails proto descriptor to be added to the pool so
// we can deserialize it.
proto::BuildDetails needed_for_proto_deserialization;

if (unpacked_inputs_) {
LoadDataFromUnpackedFile(default_filename_, visit);
} else {
Expand Down
9 changes: 9 additions & 0 deletions kythe/cxx/indexer/cxx/testdata/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,15 @@ cc_indexer_test(
],
)

cc_indexer_test(
name = "build_config",
srcs = ["basic/build_config.cc"],
ibuild_config = "test-build-config",
tags = [
"basic",
],
)

test_suite(
name = "indexer_basic",
tags = ["basic"],
Expand Down
5 changes: 5 additions & 0 deletions kythe/cxx/indexer/cxx/testdata/basic/build_config.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// Test that `/kythe/build/config` is present on anchor nodes when
/// included in the compilation unit.

//- @fn.build/config "test-build-config"
void fn();
1 change: 1 addition & 0 deletions tools/build_rules/verifier_test/cc_indexer_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ _INDEXER_FLAGS = {
"fail_on_unimplemented_builtin": True,
"ignore_unimplemented": False,
"index_template_instantiations": True,
"ibuild_config": "",
}

def _compiler_options(ctx, cpp, copts, includes):
Expand Down

0 comments on commit 96c7d6b

Please sign in to comment.