Skip to content

Commit 98766d2

Browse files
vonosmaserichkeane
andauthored
[clang] Cleanup docs and code for legacy no_sanitize attributes (NFC). (llvm#161311)
Update generated docs for legacy attributes: * no_sanitize_(address|thread|memory) * no_address_safety_analysis Those are older forms of no_sanitize("list", "of", "sanitizers") attribute. They were previously as various spellings of the same attribute, which made the auto-generated documentation confusing. Fix this by explicitly making them three different attributes. This would also allow to simplify the delegation to the new no_sanitize form slightly, as we can instead rely on auto-generated code to check that TSan and MSan can't be disabled for globals. **HTML docs before:** <img width="1004" height="1175" alt="rendered-docs-before" src="https://github.com/user-attachments/assets/407b5fc1-799c-4882-8ff8-44a5ef3cf4f1" /> **HTML docs after:** <img width="1098" height="1118" alt="rendered-docs-after" src="https://github.com/user-attachments/assets/236ca93f-25f8-4d58-95ac-ede95ce18d01" /> --------- Co-authored-by: Erich Keane <[email protected]>
1 parent 0c1e7cc commit 98766d2

File tree

5 files changed

+65
-30
lines changed

5 files changed

+65
-30
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ Moved checkers
583583

584584
Sanitizers
585585
----------
586+
- Improved documentation for legacy ``no_sanitize`` attributes.
586587

587588
Python Binding Changes
588589
----------------------

clang/include/clang/Basic/Attr.td

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3921,16 +3921,31 @@ def NoSanitize : InheritableAttr {
39213921
}];
39223922
}
39233923

3924-
// Attributes to disable a specific sanitizer. No new sanitizers should be added
3924+
// Attribute to disable AddressSanitizer. No new spellings should be added
39253925
// to this list; the no_sanitize attribute should be extended instead.
3926-
def NoSanitizeSpecific : InheritableAttr {
3926+
def NoSanitizeAddress : InheritableAttr {
39273927
let Spellings = [GCC<"no_address_safety_analysis">,
3928-
GCC<"no_sanitize_address">,
3929-
GCC<"no_sanitize_thread">,
3930-
Clang<"no_sanitize_memory">];
3928+
GCC<"no_sanitize_address">];
39313929
let Subjects = SubjectList<[Function, GlobalVar], ErrorDiag>;
3932-
let Documentation = [NoSanitizeAddressDocs, NoSanitizeThreadDocs,
3933-
NoSanitizeMemoryDocs];
3930+
let Documentation = [NoSanitizeAddressDocs];
3931+
let ASTNode = 0;
3932+
}
3933+
3934+
// Attribute to disable ThreadSanitizer. No new spellings should be added
3935+
// to this list; the no_sanitize attribute should be extended instead.
3936+
def NoSanitizeThread : InheritableAttr {
3937+
let Spellings = [GCC<"no_sanitize_thread">];
3938+
let Subjects = SubjectList<[Function], ErrorDiag>;
3939+
let Documentation = [NoSanitizeThreadDocs];
3940+
let ASTNode = 0;
3941+
}
3942+
3943+
// Attribute to disable MemorySanitizer. No new spellings should be added
3944+
// to this list; the no_sanitize attribute should be extended instead.
3945+
def NoSanitizeMemory : InheritableAttr {
3946+
let Spellings = [Clang<"no_sanitize_memory">];
3947+
let Subjects = SubjectList<[Function], ErrorDiag>;
3948+
let Documentation = [NoSanitizeMemoryDocs];
39343949
let ASTNode = 0;
39353950
}
39363951

clang/include/clang/Basic/AttrDocs.td

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
// version control.
1414
//
1515
// To run clang-tblgen to generate the .rst file:
16-
// clang-tblgen -gen-attr-docs -I <root>/llvm/tools/clang/include
17-
// <root>/llvm/tools/clang/include/clang/Basic/Attr.td -o
18-
// <root>/llvm/tools/clang/docs/AttributeReference.rst
16+
// clang-tblgen -gen-attr-docs -I <root>/clang/include
17+
// <root>/clang/include/clang/Basic/Attr.td -o
18+
// <root>/clang/docs/AttributeReference.rst
1919
//
2020
// To run sphinx to generate the .html files (note that sphinx-build must be
2121
// available on the PATH):
2222
// Windows (from within the clang\docs directory):
2323
// make.bat html
24-
// Non-Windows (from within the clang\docs directory):
25-
// sphinx-build -b html _build/html
24+
// Non-Windows (from within the clang/docs directory):
25+
// sphinx-build -b html . _build/html
2626

2727
def GlobalDocumentation {
2828
code Intro =[{..
@@ -3629,6 +3629,7 @@ instrumentations should not be applied.
36293629

36303630
The attribute takes a list of string literals with the following accepted
36313631
values:
3632+
36323633
* all values accepted by ``-fno-sanitize=``;
36333634
* ``coverage``, to disable SanitizerCoverage instrumentation.
36343635

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6361,19 +6361,8 @@ static void handleNoSanitizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
63616361
Sanitizers.size()));
63626362
}
63636363

6364-
static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
6365-
const ParsedAttr &AL) {
6366-
StringRef AttrName = AL.getAttrName()->getName();
6367-
normalizeName(AttrName);
6368-
StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName)
6369-
.Case("no_address_safety_analysis", "address")
6370-
.Case("no_sanitize_address", "address")
6371-
.Case("no_sanitize_thread", "thread")
6372-
.Case("no_sanitize_memory", "memory");
6373-
if (isGlobalVar(D) && SanitizerName != "address")
6374-
S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
6375-
<< AL << AL.isRegularKeywordAttribute() << ExpectedFunction;
6376-
6364+
static AttributeCommonInfo
6365+
getNoSanitizeAttrInfo(const ParsedAttr &NoSanitizeSpecificAttr) {
63776366
// FIXME: Rather than create a NoSanitizeSpecificAttr, this creates a
63786367
// NoSanitizeAttr object; but we need to calculate the correct spelling list
63796368
// index rather than incorrectly assume the index for NoSanitizeSpecificAttr
@@ -6383,11 +6372,32 @@ static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D,
63836372
// getSpelling() or prettyPrint() on the resulting semantic attribute object
63846373
// without failing assertions.
63856374
unsigned TranslatedSpellingIndex = 0;
6386-
if (AL.isStandardAttributeSyntax())
6375+
if (NoSanitizeSpecificAttr.isStandardAttributeSyntax())
63876376
TranslatedSpellingIndex = 1;
63886377

6389-
AttributeCommonInfo Info = AL;
6378+
AttributeCommonInfo Info = NoSanitizeSpecificAttr;
63906379
Info.setAttributeSpellingListIndex(TranslatedSpellingIndex);
6380+
return Info;
6381+
}
6382+
6383+
static void handleNoSanitizeAddressAttr(Sema &S, Decl *D,
6384+
const ParsedAttr &AL) {
6385+
StringRef SanitizerName = "address";
6386+
AttributeCommonInfo Info = getNoSanitizeAttrInfo(AL);
6387+
D->addAttr(::new (S.Context)
6388+
NoSanitizeAttr(S.Context, Info, &SanitizerName, 1));
6389+
}
6390+
6391+
static void handleNoSanitizeThreadAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6392+
StringRef SanitizerName = "thread";
6393+
AttributeCommonInfo Info = getNoSanitizeAttrInfo(AL);
6394+
D->addAttr(::new (S.Context)
6395+
NoSanitizeAttr(S.Context, Info, &SanitizerName, 1));
6396+
}
6397+
6398+
static void handleNoSanitizeMemoryAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
6399+
StringRef SanitizerName = "memory";
6400+
AttributeCommonInfo Info = getNoSanitizeAttrInfo(AL);
63916401
D->addAttr(::new (S.Context)
63926402
NoSanitizeAttr(S.Context, Info, &SanitizerName, 1));
63936403
}
@@ -7513,8 +7523,14 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
75137523
case ParsedAttr::AT_NoSanitize:
75147524
handleNoSanitizeAttr(S, D, AL);
75157525
break;
7516-
case ParsedAttr::AT_NoSanitizeSpecific:
7517-
handleNoSanitizeSpecificAttr(S, D, AL);
7526+
case ParsedAttr::AT_NoSanitizeAddress:
7527+
handleNoSanitizeAddressAttr(S, D, AL);
7528+
break;
7529+
case ParsedAttr::AT_NoSanitizeThread:
7530+
handleNoSanitizeThreadAttr(S, D, AL);
7531+
break;
7532+
case ParsedAttr::AT_NoSanitizeMemory:
7533+
handleNoSanitizeMemoryAttr(S, D, AL);
75187534
break;
75197535
case ParsedAttr::AT_GuardedBy:
75207536
handleGuardedByAttr(S, D, AL);

clang/test/Misc/pragma-attribute-supported-attributes-list.test

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@
126126
// CHECK-NEXT: NoProfileFunction (SubjectMatchRule_function)
127127
// CHECK-NEXT: NoRandomizeLayout (SubjectMatchRule_record)
128128
// CHECK-NEXT: NoSanitize (SubjectMatchRule_function, SubjectMatchRule_objc_method, SubjectMatchRule_variable_is_global)
129-
// CHECK-NEXT: NoSanitizeSpecific (SubjectMatchRule_function, SubjectMatchRule_variable_is_global)
129+
// CHECK-NEXT: NoSanitizeAddress (SubjectMatchRule_function, SubjectMatchRule_variable_is_global)
130+
// CHECK-NEXT: NoSanitizeMemory (SubjectMatchRule_function)
131+
// CHECK-NEXT: NoSanitizeThread (SubjectMatchRule_function)
130132
// CHECK-NEXT: NoSpeculativeLoadHardening (SubjectMatchRule_function, SubjectMatchRule_objc_method)
131133
// CHECK-NEXT: NoSplitStack (SubjectMatchRule_function)
132134
// CHECK-NEXT: NoStackProtector (SubjectMatchRule_function)

0 commit comments

Comments
 (0)