2 * Copyright 2004-present Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #include <folly/experimental/logging/xlog.h>
17 #include <folly/Synchronized.h>
19 using folly::StringPiece;
25 * buck copies header files from their original location in the source tree
26 * and places them under buck-out/ with a path like
27 * buck-out/<rule-name-components>/<original-path>
29 * We want to strip off the buck-out/<rule-name-components> portion,
30 * so that the filename we use is just the original path in the source tree.
32 * The <rule-name-component> section should always end in a path component that
33 * includes a '#': it's format is <rule-name>#<parameters>, where <parameters>
34 * is a comma separated list that never includes '/'.
36 * Search for the first path component with a '#', and strip off everything up
39 StringPiece stripBuckOutPrefix(StringPiece filename) {
42 auto end = filename.find('/', idx);
43 if (end == StringPiece::npos) {
44 // We were unable to find where the buck-out prefix should end.
48 auto component = filename.subpiece(idx, end - idx);
49 if (component.find('#') != StringPiece::npos) {
50 return filename.subpiece(end + 1);
55 } // unnamed namespace
57 std::string getXlogCategoryNameForFile(StringPiece filename) {
58 // Buck mangles the directory layout for header files. Rather than including
59 // them from their original location, it moves them into deep directories
60 // inside buck-out, and includes them from there.
62 // If this path looks like a buck header directory, try to strip off the
63 // buck-specific portion.
64 if (filename.startsWith("buck-out/")) {
65 filename = stripBuckOutPrefix(filename);
68 std::string categoryName = filename.str();
70 // Translate slashes to dots, to turn the directory layout into
71 // a category hierarchy.
72 size_t lastDot = std::string::npos;
73 for (size_t n = 0; n < categoryName.size(); ++n) {
74 if (categoryName[n] == '/') {
75 categoryName[n] = '.';
76 lastDot = std::string::npos;
77 } else if (categoryName[n] == '.') {
82 // Strip off the filename extension, if one was present.
83 if (lastDot != std::string::npos) {
84 categoryName.resize(lastDot);